|
|
Abstract class |
Interface |
|
Instantiation of |
No |
No |
|
Class |
An inheritance relationship in which a class can use only one inheritance relationship. Multiple inheritance can be implemented by inheriting several interfaces |
A class can implement multiple interface |
|
Data members |
can have their own |
Static cannot be modified that must be static final, which is generally not defined here |
|
Method |
Can be private, non-abstract methods, must be implemented |
Not private, default is Public,abstract type |
|
Variable |
Can have private, default is friendly type, its value can be redefined in subclass, can also be re-assigned value |
Not private, default is public static final type, and must give its initial value, the implementation class can not be redefined, cannot change its values. |
|
Design concept |
Represents a "is-a" relationship |
Represents a "like-a" relationship |
|
Realize |
Need to inherit, to use extends |
To use implements |
Where to use the interface
A. Classes and classes require a specific interface to reconcile, rather than how they are implemented.
B. As an identity that is capable of implementing a specific function, it can also be a purely identifying interface method that does not exist.
C. A set of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.
D. A specific number of functions need to be implemented, and there may be no connection between these features at all.
What is the use of abstract classes?
In a word, it can be used in situations where both a uniform interface is required and an instance variable or a default method is required. The most common are:
A. Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use the abstract class to define a set of method bodies, even an empty method body, and then select the methods that you are interested in to overwrite by subclasses.
B. In some cases, a purely interface does not satisfy the coordination between classes and classes, and the variables that represent states in a class must differentiate between different relationships. Abstract mediation can be a good way to meet this point.
C. Standardize a set of mutually coordinated methods, some of which are common, state-independent, shareable, and do not need subclasses to implement separately, while others require individual subclasses to implement specific functions according to their own specific state.
The difference between Java interfaces and abstract classes