[CCID-It Technology Report] abstractclass and interface are two types of mechanisms supported for the definition of abstract classes in Java, precisely because of the existence of these two mechanisms, it gives Java powerful object-oriented capabilities. Abstractclass and interface have great similarity in support for abstract class definitions, and can even be replaced with each other. Therefore, many developers are relatively casual in selecting abstractclass and interface when defining abstract classes. In fact, there is a big difference between the two. Their choices even reflect the understanding of the nature of the problem domain, and whether the understanding of the design intent is correct and reasonable. This article will analyze the differences between them and try to provide developers with a basis for selection between them.
Understanding abstract classes
Abstractclass and interface are used for abstract classes in Java. (The abstract classes in this article are not translated from abstractclass. They represent an abstract body, abstractclass is a method used to define abstract classes in the Java language. Be careful when defining them.) So what is an abstract class? What are the benefits of using abstract classes?
In the concept of object-oriented, we know that all objects are depicted through classes, but this is not the case. Not all classes are used to depict objects. If a class does not contain enough information to depict a specific object, such classes are abstract classes. Abstract classes are often used to represent the abstract concepts we have come up with in the analysis and design of problem domains. They are abstractions of a series of seemingly different but essentially identical specific concepts. For example, if we develop a graphic editing software, we will find that some specific concepts such as circles and triangles exist in the problematic domain. They are different, however, they all belong to the concept of shape. The concept of shape does not exist in the field of problem. It is an abstract concept. Abstract concepts cannot be instantiated because they do not have specific concepts in the problem field.
In the Object-Oriented field, abstract classes are mainly used to hide types. We can construct a fixed abstract description of a group of actions, but this group of actions can have any specific implementation method. This abstract description is an abstract class, and any possible implementations of this group are represented as all possible Derived classes. The module can operate on an abstract body. Because the module depends on a fixed abstract body, it may not be allowed to be modified. At the same time, the behavior function of this module can be extended by deriving from this abstract body. Readers who are familiar with OCP must know that abstract classes are the key to implementing the Open-closedprinciple principle of object-oriented design.
View abstractclass and interface in terms of syntax definition
At the syntax level, the Java language provides different definitions for abstractclass and interface. The following describes how to define an abstract class named demo.
The following describes how to use abstractclass to define a demo abstract class:
abstractclassDemo{abstractvoidmethod1();abstractvoidmethod2();…} |
The following method is used to define the demo abstract class using the interface:
interfaceDemo{voidmethod1();voidmethod2();…} |
In the abstractclass method, the demo can have its own data members or non-Abstarct member methods. In the implementation of the interface method, demo can only have static data members that cannot be modified (that is, they must be staticfinal, but generally do not define data members in the interface). All member methods are abstract. In a sense, interface is a special form of abstractclass.
From the programming point of view, abstractclass and interface can be used to implement the idea of "designbycontract. However, there are some differences in usage.
First, abstractclass represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces. Maybe this is a compromise between Java designers and Java's support for multi-inheritance.
Secondly, in the definition of abstractclass, we can assign the default behavior of the method. However, in the interface definition, a method cannot have default behavior. to bypass this restriction, you must use a delegate. However, this increases complexity and sometimes causes great trouble.
Another serious problem still exists when the default behavior cannot be defined in the abstract class, which may cause maintenance trouble. Because if you want to modify the interface of the class (usually expressed by abstractclass or interface) to adapt to the new situation (for example, adding a new method or adding a new parameter to the used method) it will be very troublesome and may take a lot of time (especially when there are many derived classes ). However, if the interface is implemented through abstractclass, you may only need to modify the default behavior defined in abstractclass.
Similarly, if the default behavior cannot be defined in the abstract class, the same method will appear in every derived class of the abstract class. This violates the "onerule, oneplace" principle and leads to code duplication, it is not conducive to future maintenance. Therefore, be careful when selecting abstractclass and interface.
Design Concepts: abstractclass and Interface
The above mainly discusses the differences between abstractclass and interface from the perspective of syntax definition and programming. The differences between these levels are relatively low-level and non-essential. This section analyzes the differences between abstractclass and interface on the other layer. The author believes that only by analyzing at this level can we understand the essence of the two concepts.
As mentioned above, abstarctclass represents an inheritance relationship in Java. To make the inheritance relationship reasonable, there must be an "Isa" relationship between the parent class and the derived class, that is to say, the concept of the parent class and the derived class should be essentially the same (for more information, see [3 ). For an interface, it is not required that the implementer of the interface and the interface definition are essentially consistent in concept, but only implement the contract defined by the interface. In order to make the discussion easier to understand, we will explain it through a simple example below.
Consider this example. Suppose there is an abstract concept about the door in our problem field. The door has two actions: open and close, in this case, we can use abstractclass or interface to define a type that represents the abstract concept. The definitions are as follows:
Define the door using abstractclass:
abstractclassDoor{abstractvoidopen();abstractvoidclose();} |
Use the interface method to define the door:
interfaceDoor{voidopen();voidclose();} |
For other specific door types, you can use extends to define the door or implements using the interface method. It seems that there is no big difference between using abstractclass and interface.
If you want the door to have the alarm function. How can we design the class structure for this example (in this example, we mainly want to demonstrate the differences between abstractclass and interface in the design concept, other irrelevant issues have been simplified or ignored )? Below we will list possible solutions and analyze these different solutions from the design concept layer.
Solution 1:
Add an alarm method to the door definition as follows:
abstractclassDoor{abstractvoidopen();abstractvoidclose();abstractvoidalarm();} |
Or
interfaceDoor{voidopen();voidclose();voidalarm();} |
The alarmdoor with alarm function is defined as follows:
classAlarmDoorextendsDoor{voidopen(){…}voidclose(){…}voidalarm(){…}} |
Or
classAlarmDoorimplementsDoor{voidopen(){…}voidclose(){…}voidalarm(){…}} |
This method violates a core principle ISP (interfacesegregationpriciple) in object-oriented design ), in the definition of door, the inherent behavior methods of the door concept are mixed with the behavior methods of another concept "alarm. One problem is that the modules that rely solely on the door concept will change due to changes in the concept of "alarm" (for example, modifying the parameters of the alarm method.
Solution 2:
Since open, close, and alarm belong to two different concepts, they should be defined in abstract classes that represent these two concepts according to the ISP principle. The two concepts are defined by using abstractclass. Both concepts are defined by interface. One is defined by abstractclass, and the other is defined by interface.
Obviously, because the Java language does not support multiple inheritance, it is not feasible to define both concepts using abstractclass. The latter two methods are feasible, but their selection reflects the understanding of the concept nature in the problem field, and whether the reflection of the design intent is correct and reasonable. Let's analyze and explain them one by one.
If both concepts are defined using the interface method, there are two problems: 1. We may not understand the problem field clearly. Is the concept of alarmdoor actually a door or an alarm? 2. If we have no problem in understanding the problem field, for example, we have found that alarmdoor is essentially consistent with door through analysis of the problem field, therefore, our design intent cannot be correctly revealed during implementation, because the definitions of these two concepts (both using the interface method definition) do not reflect the above meaning.
If our understanding of the problem field is: alarmdoor is essentially a door in concept, it also has the alarm function. How can we design and implement it to clearly reflect what we mean? As mentioned above, abstractclass represents an inheritance relation in Java, and the inheritance relation is essentially an "Isa" relation. So we should use the abstarctclass method to define the concept of door. In addition, alarmdoor has the alarm function, indicating that it can complete the behaviors defined in the alarm concept. Therefore, the alarm concept can be defined through interface. As follows:
abstractclassDoor{ abstractvoidopen(); abstractvoidclose(); } interfaceAlarm{ voidalarm(); } classAlarmDoorextendsDoorimplementsAlarm{ voidopen(){ … } voidclose(){ … } voidalarm(){ … } } |
This implementation method can clearly reflect our understanding of the problem field and correctly reveal our design intent. Actually, abstractclass indicates the "Isa" relationship, and interface indicates the "likea" relationship. You can use it as a basis for your selection. Of course, this is based on your understanding of the problem field, for example, if we think that alarmdoor is essentially an alarm and has a door function, the above definition will be reversed.