Java中抽象类和接口之间的区别

先决条件–接口, 抽象类
【Java中抽象类和接口之间的区别】抽象:隐藏功能的内部实现, 仅向用户显示功能。即它的工作原理(显示), 工作原理(隐藏)。都抽象类和接口用于抽象。
抽象类与接口

  1. 方法类型:接口只能具有抽象方法。抽象类可以具有抽象和非抽象方法。从Java 8开始, 它还可以具有默认方法和静态方法。
  2. 最终变量:在Java接口中声明的变量默认为final。抽象类可能包含非最终变量。
  3. 变量类型:抽象类可以具有最终变量, 非最终变量, 静态变量和非静态变量。接口只有静态变量和最终变量。
  4. 实现抽象类可以提供接口的实现。接口无法提供抽象类的实现。
  5. 继承与抽象:可以使用关键字” implements” 来实现Java接口, 而可以使用关键字” extends” 来扩展抽象类。
  6. 多种实现:一个接口只能扩展另一个Java接口, 一个抽象类可以扩展另一个Java类并实现多个Java接口。
  7. 数据成员的可访问性:默认情况下, Java接口的成员是公共的。 Java抽象类可以具有类成员, 例如private, protected等。
Java中抽象类和接口之间的区别

文章图片
//Java program to illustrate the //concept of abstract classimport java.io.*; //abstract class abstract class Shape { //declare fields String objectName = " " ; Shape(String name) { this .objectName = name; }//declare non-abstract methods //it has default implementation public void moveTo( int x, int y) { System.out.println( this .objectName + " " + "has been moved to" + " x = " + x + " and y = " + y); }//abstract methods which will be //implemented by its subclass(es) abstract public double area(); abstract public void draw(); }class Rectangle extends Shape {int length, width; //constructor Rectangle( int length, int width, String name) {super (name); this .length = length; this .width = width; }@Override public void draw() { System.out.println( "Rectangle has been drawn " ); }@Override public double area() { return ( double )(length*width); } } class Circle extends Shape {double pi = 3.14 ; int radius; //constructor Circle( int radius, String name) {super (name); this .radius = radius; }@Override public void draw() {System.out.println( "Circle has been drawn " ); }@Override public double area() { return ( double )((pi*radius*radius)/2 ); } }class GFG { public static void main (String[] args) {//creating the Object of Rectangle class //and using shape class reference. Shape rect = new Rectangle( 2 , 3 , "Rectangle" ); System.out.println( "Area of rectangle: " + rect.area()); rect.moveTo( 1 , 2 ); System.out.println( " " ); //creating the Objects of circle class Shape circle = new Circle( 2 , "Cicle" ); System.out.println( "Area of circle: " + circle.area()); circle.moveTo( 2 , 4 ); } }

输出如下:
Area of rectangle: 6.0Rectangle has been moved to x = 1 and y = 2 Area of circle: 6.28Cicle has been moved to x = 2 and y = 4

在这种情况下, 矩形和圆形之间没有任何通用代码, 因此可以使用界面。
看到这个……
//Java program to illustrate the //concept of interface import java.io.*; interface Shape { //abstract method void draw(); double area(); }class Rectangle implements Shape { int length, width; //constructor Rectangle( int length, int width) { this .length = length; this .width = width; }@Override public void draw() { System.out.println( "Rectangle has been drawn " ); }@Override public double area() { return ( double )(length*width); } } class Circle implements Shape {double pi = 3.14 ; int radius; //constructor Circle( int radius) {this .radius = radius; }@Override public void draw() { System.out.println( "Circle has been drawn " ); }@Override public double area() {return ( double )((pi*radius*radius)/2 ); }}class GFG { public static void main (String[] args) {//creating the Object of Rectangle class //and using shape interface reference. Shape rect = new Rectangle( 2 , 3 ); System.out.println( "Area of rectangle: " + rect.area()); //creating the Objects of circle class Shape circle = new Circle( 2 ); System.out.println( "Area of circle: " + circle.area()); } }

输出如下
Area of rectangle: 6.0Area of circle: 6.28

什么时候使用什么?
如果以下任何一种情况适用于你的情况, 请考虑使用抽象类:
  • 在Java应用程序中, 有一些相关的类需要共享一些代码行, 然后可以将这些代码行放入抽象类中, 并且所有这些相关类都应扩展此抽象类。
  • 你可以在抽象类中定义非静态或非最终字段, 以便通过一种方法可以访问和修改它们所属的对象的状态。
  • 你可以期望扩展抽象类的类具有许多公共方法或字段, 或者需要除public(例如protected和private)之外的访问修饰符。
如果以下任何一种情况适用于你的情况, 请考虑使用接口:
  • 这是完全抽象的, 在接口中声明的所有方法都必须由实现此接口的类来实现。
  • 一个类可以实现多个接口。这称为多重继承。
  • 你想指定特定数据类型的行为, 但不关心谁实现了它的行为。
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

    推荐阅读