abstractclass and interface are two mechanisms in the Java language that support abstract class definitions. It is precisely because of the existence of these two mechanisms that Java is given powerful object-oriented capabilities. There are great similarities between abstractclass and interface in terms of support for abstract class definition, and they can even be replaced with each other. Therefore, many developers seem to be more casual about the choice of abstractclass and interface when defining abstract classes. In fact, there is still a big difference between the two. The choice of them even reflects the understanding of the nature of the problem domain and whether the understanding of the design intention is correct and reasonable. This article will analyze the differences between them and try to provide developers with a basis for choosing between the two.
Understand abstract classes
Abstractclass and interface are both used to create abstract classes in the Java language (the abstract class in this article is not translated from abstractclass, it represents an abstract body, and abstractclass is a method used to define abstract classes in the Java language) , readers please pay attention to distinguish) definition, then what is an abstract class, and what benefits can using abstract classes bring us?
In the object-oriented concept, we know that all objects are represented by classes, but the reverse is not true. Not all classes are used to describe objects. If a class does not contain enough information to describe a specific object, such a class is an abstract class. Abstract classes are often used to represent abstract concepts that we derive from analysis and design of problem areas. They are abstractions of a series of specific concepts that look different but are essentially the same. For example: If we develop a graphics editing software, we will find that there are some specific concepts such as circles and triangles in the problem domain. They are different, but they all belong to the concept of shape. The concept of shape is different in the problem domain. If it exists, it is an abstract concept. It is precisely because abstract concepts do not have corresponding concrete concepts in the problem domain, so the abstract classes used to represent abstract concepts cannot be instantiated.
In the object-oriented field, abstract classes are mainly used for type hiding. We can construct an abstract description of a fixed set of behaviors, but this set of behaviors can have any number of possible concrete implementations. This abstract description is the abstract class, and this set of any possible concrete implementations is represented by all possible derived classes. Modules can operate on an abstract body. Because a module relies on a fixed abstraction, it cannot be modified; at the same time, the behavior of this module can also be extended by deriving from this abstraction. Readers who are familiar with OCP must know that in order to realize OCP (Open-ClosedPrinciple), one of the core principles of object-oriented design, abstract classes are the key.
Looking at abstractclass and interface from the grammatical definition level
At the grammatical level, the Java language provides different definitions for abstractclass and interface. The following is an example of defining an abstract class named Demo to illustrate this difference.
The way to define the Demo abstract class using abstractclass is as follows:
From a programming perspective, both abstractclass and interface can be used to implement the idea of "designbycontract". However, there are still some differences in specific usage.
First of all, abstractclass represents an inheritance relationship in the Java language, and a class can only use the inheritance relationship once. However, a class can implement multiple interfaces. Perhaps this is a compromise consideration by the designers of the Java language when considering Java's support for multiple inheritance.
Secondly, in the definition of abstractclass, we can assign the default behavior of the method. But in the definition of interface, methods cannot have default behavior. In order to circumvent this restriction, delegates must be used, but this will add some complexity and sometimes cause a lot of trouble.
There is another serious problem in not being able to define default behavior in an abstract class, which is that it may cause maintenance trouble. Because if you later want to modify the interface of the class (usually represented by abstractclass or interface) to adapt to new situations (for example, add new methods or add new parameters to existing methods), it will be very troublesome, and it may It takes a lot of time (especially when there are many derived classes). But if the interface is implemented through abstractclass, then you may only need to modify the default behavior defined in abstractclass.
Similarly, if the default behavior cannot be defined in an abstract class, the same method implementation will appear in every derived class of the abstract class, violating the "one rule, one place" principle, resulting in code duplication, which is also not conducive to future maintenance. . Therefore, be very careful when choosing between abstractclass and interface.
Looking at abstractclass and interface from the perspective of design concepts. The above mainly discusses the differences between abstractclass and interface from the perspective of grammatical definition and programming. The differences at these levels are relatively low-level and non-essential. This section will analyze the difference between abstractclass and interface from another level: the design concepts reflected by the two. The author believes that