Abstract Class and Interface are two mechanisms for the support of abstract class definitions in the Java language, which is due to the existence of these two mechanisms, which gives Java powerful object-oriented ability. Abstract Class and Interface have great similarities between support for abstract class definitions, even replaced with each other, so many developers are more casual for Abstract Class and Interface when performing abstract class definitions.
In fact, there is still a big difference between the two. For their choice but even reflect the understanding of the nature of the problem, it is correct and reasonable for the understanding of the design intent. This article will analyze the differences between them, trying to provide the developer to provide a basis for choice between the two.
First, understand the abstract class
Abstract Class and Interface are used in the Java language (the abstract classes in this article are not translated from Abstract Class, which represents an abstract body, and Abstract Class is used to define abstract classes in Java languages. One way, please pay attention to distinguish the definition, then what is the benefit of the abstract class?
In the object-oriented concept, we know that all objects are drawn by classes, but it is not the case. Not all classes are used to draw objects, if there is no sufficient information in a class to depict a specific object, such a class is an abstract class. Abstract classes are often used to characterize our abstraction concepts in the design of the problem. The abstraction of the design is different, but the same concrete concept is in nature.
For example, if we develop a graphic editing software, there will be a circle in the problem area, and there are some specific concepts such as the triangle, which are different, but they are in the shape of the shape, the shape of this concept is in the problem area. Present, it is an abstract concept. It is precisely because the concept of abstraction does not correspond to the specific concept in the problem area, so the abstract class used to characterize the abstraction concept is not instantiated.
In the object-oriented domain, the abstract class is mainly used to hide the type. We can construct an abstract description of a fixed set of behaviors, but this set of behavior can have any possible specific implementation. This abstract description is an abstraction class, and this group of any possible specific implementations are manifestative as all possible derived classes. The module can operate an abstract body. Since the module depends on a fixed abstraction, it can be modified; at the same time, the behavior function of this module can be extended by derived from this abstract body. Readers who are familiar with OCP must know that in order to enable object-oriented design, an OCP (Open -Closed Principle), the abstract class is the key.
Second, look at Abstract Class and Interface from the grammatical definition level
At the grammatical level, the Java language gives different definitions for Abstract Class and Interface, below to define an abstract class named DEMO as an example to illustrate this difference. The way the Demo abstraction class is defined using the Abstract Class.
Abstract Class demo {
Abstract void method1 ();
Abstract void method2 ();
...
}
The way the Demo abstraction class is defined as the interfab is as follows:
Interface demo {
Void method1 ();
Void method2 ();
...
}
In the Abstract Class method, Demo can have its own data member, or there is a non-AbStarct member method, and in the implementation of the Interface mode, DEMO can only have static data members (which must be static final. However, in Interface generally does not define data members), all member methods are Abstract. In a sense, Interface is a special form of Abstract Class. From a programming perspective, Abstract Class and Interface can be used to implement the idea of "Design By Contract". But there are still some differences in specific use. First, the Abstract Class represents a inheritance relationship in the Java language, and a class can only use one inheritance relationship. However, a class can implement multiple interface. Perhaps this is a collection of Java language designers who consider Java for multiple inheritance support. Second, in the definition of Abstract Class, we can give the default behavior of the method. However, in the definition of Interface, the method cannot have default behavior. In order to bypass this limit, it must be entrusted, but this will increase some complexity and sometimes cause a lot of trouble. There is another serious problem that cannot be defined in an abstract class, which may cause trouble on maintenance. Because if you want to modify the interface of the class (generally represented by Abstract Class or Interface) to accommodate new conditions (for example, add new methods or add new parameters to the used methods), it will be very troublesome. It may take a lot of time (especially the case, which is much more likely to derive.). However, if the interface is implemented by Abstract Class, it is possible to only need to modify the default behavior defined in the Abstract CLASS. Similarly, if the default behavior cannot be defined in the abstract class, it will cause the same method to achieve the "One Rule, One Place" principle, causing the code repetition, which is equally not conducive to the principle of "One Rule, One Place" principle. Maintenance. Therefore, it is very careful when choosing between Abstract Class and Interface. Third, from the design concept level, Abstract Class and Interface mainly discusses the difference from the Abstract Class and Interface from the perspective of syntax definitions and programming. The difference between these levels is relatively low, non-essential. This article will analyze the difference between the two, and the design concept reflected by the Abstract Class and Interface. The author believes that from this level, it can understand the essence of the concept. As mentioned earlier, AbStarct Class reflects a inheritance in Java language. To make the inheritance relationship, "IS A" relationship must be present between the parent class and derived class, namely the parent class and derived class in the concept of the concept. The upper should be the same. For Interface, it is not required to implement the implementation of Interface and Interface definitions in nature, just a contract that implements the Interface definition. In order to make it easy to understand, the following will be described below.
Consider an example, assuming that there is an abstract concept about Door in our problem, which has two action Open and Close, which we can define a type indicating the abstraction concept through the Abstract Class or Interface. The definition method is shown below: Use the Abstract Class method to define DOOR:
Abstract class door {
Abstract void open ();
Abstract void close ();
}
Use the Interface mode to define DOOR:
Interface door {
Void open ();
Void close ();
}