One of the biggest differences between the 1.Java interface and the Java Abstract class is that Java Abstract classes can provide partial implementations of some methods, while Java interfaces cannot (that is, interface can only be defined in the method, but not the implementation of the method, and in the abstract class may be both the concrete implementation of the method, and there is no concrete implementation of the abstract method, this is probably the only advantage of Java abstract class, 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 you add a new method to a Java interface, All classes that implement this interface cannot be compiled successfully because you have to have each class implement this method again, which is obviously a disadvantage of the Java interface.
2. the implementation of an abstract class can only be given by subclasses of this abstract class, that is, the implementation is in the hierarchy of inheritance defined by the abstract class, and because of the single inheritance of the Java language, the efficiency of the abstract class as a type definition tool is greatly compromised. At this point, the advantages of theJava interface come out, any one implementation of a Java interface defined by the method of the class can have this interface type, and a class can implement any number of Java interfaces, Thus, there are many types of this class. (with abstract classes, the subclass type that inherits this abstract class is relatively single, because subclasses can only inherit abstract classes, whereas subclasses can implement multiple interfaces at the same time because the types are more numerous.) Interfaces and abstract classes can define objects, but only their specific implementation classes are instantiated. )
3. from point 2 , it is not difficult to see that theJava interface is the ideal tool for defining mixed types, and the hybrid class indicates that a class does not only have the behavior of one of the main types, but also has other minor behaviors.
4.Combine1、2Point in the abstract class andJavaThe advantages of the interface, the design pattern of the refined code is out: the work of declaring type is still byJavainterface, but at the same time gives aJavaAbstract class, and implements this interface, and other specific classes that belong to this abstract type can choose to implement thisJavaInterface, you can also choose to inherit this abstract class, meaning that in the hierarchy,Javainterface at the top, followed by the abstract class, the next two of the biggest advantages can be played to the extreme. This mode is "default adaptation mode". InJavaLanguageAPIUse this pattern, and all follow a certain naming convention:Abstract+ Interface name. (A extends ABSTRACTB implements INTERFACECSoAThat is, you can choose to implement(@Override)InterfaceINTERFACECIn the method, can also choose not to implement;AThat is, you can choose to implement(@Override)Abstract classABSTRACTBOr you can choose not to implement the method)
Java interfaces and abstract class differences