One of the biggest differences between the
1, Java interface, and Java abstract classes is that Java abstract classes can provide some implementations of some methods, while Java interfaces are not, which is probably the only advantage of Java abstract classes, but this advantage is very useful.
If you add a new concrete method to an abstract class, all of its subclasses get the new method all of a sudden, and the Java interface does not do this, and if a new method is added to a Java interface, all classes that implement the interface will fail to compile successfully. Because you have to let every class implement this method again, this is obviously a disadvantage of the Java interface.
2, the implementation of an abstract class can only be given by the subclass of this abstract class, that is, the implementation in the abstract class is defined by the hierarchical structure of inheritance, and because of the Java language single inheritance, so the abstract class as a type definition tool performance is greatly compromised.
At this point, the advantages of the Java interface comes out that any class that implements a method specified by a Java interface can have the type of this interface, and a class can implement any number of Java interfaces, so there are many types of this class.
3, from the 2nd, it is easy to see that the Java interface is the ideal tool for defining mixed types, and the hybrid class indicates that a class has not only the behavior of one of the main types, but also other minor behaviors.
4, combining the advantages of abstract classes and Java interfaces in 1, 2 points, a refined design pattern comes out: the work of declaring type is still assumed by the Java interface, but at the same time a Java abstract class is given, and this interface is implemented. The other class of the same abstract type can choose to implement this Java interface, you can choose to inherit this abstract class, that is, in the hierarchy, the Java interface on the top, followed by the abstract class, ha, the next two of the greatest advantages can be played to the extreme. This mode is "default adaptation mode". The
uses this pattern in the Java language API, and all follows a certain naming convention: the Abstract + interface name.
Java interfaces and Java abstract classes exist to be used for the implementation and inheritance of specific classes, and if you are going to write a specific class to inherit another specific class, then your design has a big problem. The Java abstract class exists for inheritance, and its abstract approach is to force subclasses to be implemented. The
uses Java interfaces and abstract Java classes to declare variable types, arguments are type declarations, method return type descriptions, and conversion of data types. Instead of using specific Java classes to declare variable types, arguments are type declarations, method return type descriptions, and data type conversions.
Differences between Java abstract classes and Interfaces (reprinted)