This article describes the differences between abstract and interface in Java. abstract class and interface are two mechanisms supported for the definition of abstract classes in Java, the existence of these two mechanisms gives 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 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 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 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.
You can use abstract class to define a Demo abstract class as follows:
The Code is as follows: |
Copy code |
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 abstract class mode, Demo can have abstruct data members or non-abstarct member methods. In interface implementation, 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 interface of the class (usually expressed by abstract class 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 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 same method will appear in every derived class of the abstract class, in violation of the "one rule, one place" principle, code duplication is also not conducive to future maintenance. Therefore, be careful when selecting abstract class and interface.
Iii. abstract class and interface
The difference between abstract class and interface is discussed from the perspective of syntax definition and programming. The difference between these layers is 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 a "is a" relationship between the parent class and the derived class, that is, the concept of the parent class and the derived class should be essentially the same. 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:
The Code is as follows: |
Copy code |
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 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 abstract class and interface in the design concept, other irrelevant issues are simplified or ignored.) The following describes possible solutions and analyzes these solutions at the design concept layer.
Solution 1: simply add an alarm method to the Door definition, as shown below:
The Code is as follows: |
Copy code |
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:
The Code is as follows: |
Copy code |
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 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. 1 2