In Java,
Abstract class And
Interface Is two mechanisms that support the definition of abstract classes. The existence of these two mechanisms gives Java powerful object-oriented capabilities.
Abstract class And
Interface They have great similarities in support for abstract class definitions and can even be replaced with each other. Therefore, many developers
Abstract class And
Interface Is relatively casual. 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
Abstract class and interface are used for abstract classes in Java language (abstract classes in this article are not translated from abstract class, it represents an abstract body, abstract class is a method used to define abstract classes in the Java language. Please note that it is defined). So what is an abstract class and what benefits can it bring to us by 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, but they all belong to the concept of shape.Problem areaIt does not exist. 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 usedType hiding. 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. FamiliarOCPReaders must know that in order to realize a core principle of object-oriented designOCP (open-closed principle)Abstract class is the key.
Abstract class and interface in terms of syntax definition
At the syntax level, the Java language provides different definitions for abstract class and interface. The following describes how to define an abstract class named demo.
You can use abstract class to define a demo abstract class as follows:
Abstract class demo {
Abstract void Method1 ();
Abstract void method2 ();
...
}
The following method is used to define the demo abstract class using the interface:
Interface demo {
Void Method1 ();
Void method2 ();
...
}
In the abstract class method, the demo can have its own data members or non-abstract 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 static final, but generally do not define data members in the interface). All member methods are abstract. In a sense, interface is a special form of abstract class.
From the programming point of view, abstract class and interface can be used to implement"Design by contract". However, there are some differences in usage.
Abstract class represents a type in Java.InheritanceA class can only use an inheritance relationship once (Because Java does not support multi-inheritance-reinjection ). 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 abstract class, we can assignDefault behavior. However, in the interface definition, methods cannot have default behaviors. to bypass this restriction, you must useDelegateBut this will increase some complexity and sometimes cause a lot of trouble.
Another serious problem still exists when the default behavior cannot be defined in the abstract class, which may cause maintenance trouble. This is because if you want to modify the interface of the class (usually expressed by abstract class or interface) to adapt to the new situation (for example, when 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 for a lot of derived classes ). However, if the interface is implemented through abstract class, you may only need to modify the default behavior defined in abstract class.
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."One rule, one place"Principle, resulting inCodeRepetition is also not conducive to future maintenance. Therefore, be careful when selecting abstract class and interface.
Abstract class and interface from the design concept level
This section mainly discusses the differences between abstract class and interface from the perspective of syntax definition and programming. The differences between these layers are relatively low-level and non-essential. This section analyzes the differences between abstract class 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, abstract class represents an inheritance relationship in Java. To make the inheritance relationship reasonable, there must be a "is-a" 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 (references [3] have in-depth discussions about the "is-a" relationship, if you are interested, refer ). 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, abstract class or interface can be used to define a type that represents the abstract concept. The definitions are as follows:
Use abstract class to define door:
Abstract class door {
Abstract void open ();
Abstract void close ();
}
Use the interface method to define the door:
Interface door {
Void open ();
Void close ();
}
For other specific door types, extends can use the door defined in abstract class or implements to use the door defined in interface mode. It seems that there is no big difference between abstract class and interface.
If door is required, the alarm function is required. How can we design the class structure for this example (in this example, we mainly want to demonstrate the differences between abstract class 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:
Abstract class door {
Abstract void open ();
Abstract void close ();
Abstract void alarm ();
}
Or
Interface door {
Void open ();
Void close ();
Void alarm ();
}
The alarmdoor with alarm function is defined as follows:
Class alarmdoor extends door {
Void open (){...}
Void close (){...}
Void alarm (){...}
}
Or
Class alarmdoor implements door {
Void open (){...}
Void close (){...}
Void alarm (){...}
}
This method violates a core principle in object-oriented design.ISP (interface segregation principle)In the definition of the door, the inherent behavior method of the door concept is mixed with the behavior method of another concept "alarm. One problem is that the modules that rely solely on the concept of door will change because of the change in the concept of "alarm" (for example, modifying the parameters of the alarm method.
Solution 2:
Although 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 abstract class. Both concepts are defined by interface. One is defined by abstract class, and the other is defined by interface.
Obviously, because the Java language does not support multiple inheritance, both concepts are defined using abstract class. 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, through analysis of the problem field, we find that alarmdoor is essentially consistent with door in concept, 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.
For example, our understanding of the problem field is: alarmdoor is essentially a door, and it has the alarm function. How can we design and implement it to clearly reflect what we mean? As mentioned above, abstract class represents an inheritance relation in Java, and the inheritance relation is essentially a "is-a" link. So we should use the Abstarct class 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, so the alarm concept can be defined through interface. As follows:
Abstract class door {
Abstract void open ();
Abstract void close ();
}
Interface alarm {
Void alarm ();
}
Class alarm door extends door implements alarm {
Void open (){...}
Void close (){...}
Void alarm (){...}
}
This implementation method basically clearly reflects our understanding of the problem field and correctly reveals our design intent. Abstract class indicates the "is-a" relation, and interface indicates the "like-a" relation. You can use this as a basis for your selection, of course, this is based on the understanding of the problem field. For example, if we think that alarmdoor is essentially an alarm and has the door function, then the above definition method will be reversed.
Summary
1. abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces.
2. In abstract class, you can have your own data members or non-Generation of AbstarctPersonnel method, while inInterfaceMedium,OnlyStatic enough data members that cannot be modified (that is, must beStatic final, noAfterThe interface is generally not fixed.All member methods areAbstract.
3. abstract class and interface have different design concepts. Abstract class represents the "is-a" relation,Interface indicates "like-"Link.
4. classes that implement abstract classes and interfaces must implement all the methods in them. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
5. the variables defined in the interface are of the public static final type by default and must be given the initial values. Therefore, the Implementation class cannot be redefined or changed.
6. the variables in the abstract class are friendly by default. Their values can be redefined in the subclass or assigned again.
7.Method in the interfaceRecognizeAllPublic, abstractClassType.
Conclusion
Abstract class and interface are two methods of defining abstract classes in Java. They have great similarity. However, their choices often reflect the understanding of the essence of concepts in the problem field, and whether the reflection of design intent is correct and reasonable, because they represent different relationships between concepts (although they all implement the required functions ). This is actually a common use of language. I hope readers can understand it in detail.
References
[1] thinking in Java, Bruce Eckel
[2] Design Patterns explained: A New Perspective on object-oriented design, Alan shalloway and James R. Trott
[3] proactive C ++: 50 specific ways to improve your programs and design, Scott Meyers