Understand Interface and Abstract Class in Java
abstract class are similar that I was sometimes confused about their differences, or didn’t know when I should use each. There are a lot of posts about their differences in terms of usage details, but many of them do not cover differences in design. I looked up on the internet and summarized some key points I learned.
- Java 廖雪峰基础教程
- CS 61B | Part 2
- Top 50+ Core Java Interview Questions And Answers
- Java 8接口有default method后是不是可以放弃抽象类了？
First, we should explore why language designers have created these two things (understand their purposes). In essence,
actions are abstracted by interfaces, while
roots of classes are abstracted by abstract classes. By saying roots of classes, they refer to the essence or origin of those classes.
For example, in terms of eating, we can define an interface
IEat that abstracts all kinds of eat actions (eat by forks, eat by hands). It is reasonable that a class can implement multiple interfaces, say a man can have multiple ways of eating. As for abstract classes,
Woman classes can be abstracted by a
Human abstract class. We cannot instantiate a
Human class since it is abstract, but we can do that for its concrete subclasses. A class inherits from only one abstract class. We don’t allow a
Man subclassing from a
Human class and an
Animal class at the same time, which is irrational.
Second, we see how they are applied in Java class design.
List are interfaces. From this illustration, we can say that:
- Idea #1: Interfaces represent levels of abstraction.
- Idea #2: Abstract classes are used to put publicly shared logic.
- Idea #3: Abstraction levels:
Then, let’s have a look at the more complex illustration in CS 61B lecture notes.
AbstractCollection all implement
List interface. I temporarily don’t know why it is designed that way.
The takeaway of this part is to understand the three ideas above, understanding that the major role of interfaces is to define levels of abstraction.
- Both of them cannot be instantiated. Only their concrete subclasses can be done in this way.
- In an interface, default modifiers for methods are
- Both require implementations of all abstract methods. In a subclass inheriting from an abstract class, if abstract methods are not implemented, the subclass should also be defined as an abstract class. Also, all abstract methods cannot be
- A class can implement
multiple interfaces, while it can only inherit from
one abstract class.
- An interface only allows method
declaration, while an abstract class allows both
implementation. So, we can say that interfaces are for design and abstract classes are for refactoring. Marked
- An abstract class can have no abstract methods. weird
- An interface allows
static final member variables(constants), but instance variables. An abstract class can have
- An interface doesn’t have any
constructor, while an abstract class have a
default constructorand it is called whenever the concrete subclass is instantiated.
In Java 8, an interface has default methods in which we can write implementation logic. The purpose of adding this new feature is that adding new methods does not require us to modify the code in the class that implements this interface (no re-compilation).
For example, in Java SE 7, there is no
List; and in Java SE 8,
sort() is added in
List. If we had a class
List in the pass and
sort() in Java SE 8 is not a default method, we need to implement
MyList and re-compile the source file. With the help of default methods, the default implementation is provided.
MyList won’t be changed.
Also, we notice that although we have default methods in Java 8, they cannot replace abstract classes because they don’t have states (instance variables).