Object-oriented design, the focus of taking should actually be the abstraction layer of reuse, rather than a specific code block reuse.
When it comes to abstraction, I have to mention Java interfaces and Java abstract classes, which is the point I want to talk about here.
Java interfaces and abstract classes represent abstract types, which are the concrete manifestations of the abstraction layer we need to propose. OOP object-oriented programming, if you want to improve the reuse rate of programs, increase the maintainability of the program, extensibility, it must be interface-oriented programming, oriented to abstract programming, the correct use of interfaces, abstract classes of these useful abstract types as the top layer of your hierarchy.
There are so many similarities between Java interfaces and Java abstract classes, and there are so many special places, where exactly is the best place for them? Compare it and you'll find out.
(1) One of the biggest differences between Java interfaces and Java abstract classes is that Java abstract classes can provide partial implementations of some methods, while Java interfaces cannot (that is, only methods can be defined in the interface, but not the implementation of methods, but in the abstract Class can have both the specific implementation of the method, and there is no concrete implementation of the abstract method, which is probably the only advantage of Java abstract class, but this advantage is very useful. If a new concrete method is added to an abstract class, all of its subclasses get the new method at once, and the Java interface does not, 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. This means that Java's abstract analogy with Java interface is easy to extend.
(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 Java language single inheritance, so the efficiency of the abstract class as a type definition tool is greatly compromised. At one point, the advantages of Java interfaces come out, any class that implements the methods specified by the 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. (using abstract classes, the subclass type that inherits this abstract class is relatively single, because subclasses can only inherit abstract classes, and subclasses can implement multiple interfaces at the same time, so the types are much more.) Interfaces and abstract classes can define objects, but only their specific implementation classes are instantiated. )
(3) The Java interface is an ideal tool for defining mixed types, which indicates that a class does not only have the behavior of one of the main types, but also has other minor behaviors.
(4) Combining the advantages of abstract class and Java interface in (1) and (2) points, the most classic 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. Other types of the same abstract type of specific classes can choose to implement this Java interface, you can also choose to inherit this abstract class, that is, in the hierarchy, the Java interface at the top, followed by the abstract class, the next two of the greatest advantage can play to the extreme. This mode is "default adaptation mode". This pattern is used in the Java language API, and all follows a certain naming convention: the Abstract + interface name. (A extends Abstract B implements interface C), then A can choose to implement (@override) interface interface C method, you can also choose not to implement ; You can choose to implement (@override) the method of abstract class abstraction B, or you can choose not to implement)
The existence of Java interfaces and abstract classes is 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.
On the difference between Java interface and abstract class