Interface vs. abstract class

Abstraction:

Hiding the internal implementation of the feature and only showing the functionality to the users. i.e. what it works (showing), how it works (hiding). Both abstract class and interface are used for abstraction.

Abstract class vs Interface

Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
Inheritance vs Abstraction: A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.
Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

When to use what?

Consider using abstract classes if any of these statements apply to your situation:

  • In java application, there are some related classes that need to share some lines of code then you can put these lines of code within abstract class and this abstract class should be extended by all these related classes.
  • You can define non-static or non-final field(s) in abstract class, so that via a method you can access and modify the state of Object to which they belong.
  • You can expect that the classes that extend an abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).

Consider using interfaces if any of these statements apply to your situation:

  • It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this interface.
  • A class can implement more than one interface. It is called multiple inheritance.
  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.

具体而言,接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的。如果一个类中包含抽象方法,那么这个类就是抽象类。通过把类或类中的某些方法声明位 abstract(abstract 只能用类修饰类或者方法,不能修饰属性)来表示一个类是抽象类。接口是指方法的集合,接口中所有的方法都没有方法体。
包含一个或多个抽象方法的类就必须被声明为抽象类,抽象类可以声明 方法的存在而不去实现它,被声明为抽象的方法不能包含方法体。在抽象类的子类中,实现方法必须含有相同的或更低的访问级别(public > protected > private)。抽象类在使用的过程中不能被实例化,但是可以创建一个对象使其指向具体子类的一个实例。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 那些决心让激情而不是理智支配他们生活的人。这些人只受支持他们偏见的推理的影响。 那些真诚追随理智,但缺乏健全,整体...
    Fx_阅读 1,118评论 0 1
  • 最近上映的《嫌疑人X的献身》得到了不少观众的口碑和票房,相比于第一部《左耳》试水,苏有朋转型导演的第二部作品有着巨...
    简娯阅读 2,976评论 0 1
  • 晚上跟女儿聊天,我们有一搭没一搭的聊了很多,聊着聊着突然发现她真的长大了! 她偷偷的躲在被窝里哭泣,问她为什么,她...
    Cicibaby汐汐阅读 3,555评论 0 0
  • 走出电影院,外面蓝天白云,我幸庆自己活在这片和平没有战争还算美好的净土里。耳边还在嘣嘣地响着湄公河里无法消停的枪声...
    爱旖旎兔子阅读 2,773评论 0 0