Reprinted from: http://www.cnblogs.com/gxinliug/archive/2011/04/27/2031246.html
Abstract class and interface are two mechanisms supported for the definition of abstract classes in Java. It is precisely because of the existence of these two mechanisms that give Java powerful object-oriented capabilities. Abstract class and interface have great similarity in support for the definition of abstract classes, and can even be replaced with each other, therefore, when defining abstract classes, many developers may choose abstract classes and interfaces at will. 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. I. 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 object-oriented concept, we know that all objects are depicted by classes, but this is not the case in turn. 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 an abstract description of a set of fixed behaviors, but this behavior can have any specific implementation method. This abstract description is an abstract class, and the specific implementation is a derived class. 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 familiar with OCP must know that abstract classes are the key to implementing an open-closed principle Principle of object-oriented design. Ii. View 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. The following describes how to use abstract class to define demo abstract classes: abstract class demo {abstract void Method1 (); abstract void method2 ();... } Use the interface method to define the demo abstract class as follows: interface demo {void Method1 (); void method2 ();...
} In abstract class mode, the demo can have Abstruct data members or non-Abstarct member methods. In interface implementation, the demo can only have static final data members, however, data members are generally not defined in interfaces, and 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 the idea of "Design by contract. However, there are some differences in usage.
Abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship (single inheritance) at a time ). 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 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 class interface (
Class or interface, it may take a lot of time (especially when there are many 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 implementation of the same method will appear in every derived class of the abstract class, in violation of the "one rule, one place" principle, resulting in Code Repetition is also not conducive to future maintenance. Therefore
Be careful when selecting between class and interface. Iii. At the design concept level, abstract class and interface mainly discuss 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 article will analyze the differences between abstract class and interface on another level. The author believes that only by analyzing at this level can we understand the essence of the two concepts. As mentioned above, Abstarct class represents an inheritance relationship in Java. To make the inheritance relationship reasonable, there must be "is" between the parent class and the derived class.
A "relationship, that is, the parent class and the derived class are essentially the same concept. 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:
Define door: abstract class door {abstract void open (); abstract void close ();} define door: interface door {void open () using interface (); void close ();} for other specific door types, extends can use the door defined in abstract class mode or implements to use the door defined in interface mode. It seems that there is no big difference between abstract class and interface. If you want the door to have the alarm function. How do we design the class structure for this example? (in this example, we mainly want to demonstrate
The differences between class and interface are reflected in the design philosophy. Other irrelevant issues are simplified or ignored.) The following describes possible solutions, the design concept layer is used to analyze these different solutions. Solution 1: simply 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 definition of alarmdoor with alarm function is 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 priciple). In the door definition, the behavior Methods inherent in 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 in abstract class mode. Both concepts are defined in interface mode.
The class method is defined, and the other concept is defined using the interface method. 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, 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, abstract class represents an inheritance relation in Java, and the inheritance relation is essentially a "is a" relation. 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. Therefore, the alarm concept can be defined through interface. Abstract class door:
{Abstract void open (); abstract void close ();} interface alarm {void alarm ();} class alarmdoor 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 represents the "is a" relation, and interface represents the "like ".
A "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, at the same time, it has the door function, so the above definition method will be reversed. 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 concept nature in the problem field, and whether the reflection of the 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. Peach Blossom in the peach blossom dock, peach blossom under the peach blossom fairy;
Peach Blossom fairy Peach Tree, pick peach blossom and sell wine money. Just sit in front of the flowers, drunk for flowers to sleep; half awake half drunk day after day, flowers bloom year after year. I hope that the old wine room will not bow to the front of the horse; the car dust horse foot rich fun, the wine flower branches poor yuan. If the wealth is worse than the poverty, the land is in the sky; if the poverty is worse than the horse, he has to drive me idle. When others laugh at me, I laugh at others and do not wear them; when they do not see the tomb of Wuling hero, they do not have flowers or wine.