Abstract classes and interfaces distinction
Abstract: An abstract class and interface differences
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="271" height="86" align="center" valign="top"> </ td> <td width="413" valign="top"> abstract class and interface Java is the language in the definition of the abstract class support of the two mechanisms, it is precisely because the existence of these two mechanisms, it has given a powerful object-oriented Java capability. Abstract class and the interface between the abstract category in the definition of support is very similar, and even replace each other, many developers in the abstract definition of the abstract class, and the choice of interface is relatively arbitrary. In fact, between the two there is still great difference, even for their choice reflects the nature of the problem areas for understanding, the understanding of the design intent is correct and reasonable. In this paper, they will carry out the difference between a dissection, and attempt to provide developers with a choice between the two, basis.
Understand abstract class
Abstract class and interface in the Java language are used for abstract classes (This article is not in the abstract category come from the abstract class translation, it said that the body is an abstract, and abstract class for the Java language is used to define the abstract category a method to distinguish the attention of readers) definition, then what is an abstract class, the use of an abstract class can bring us any good?
</ Td> </ tr> <tr> <td height="14" colspan="2">
</ Td> </ tr> </ table>
In the object-oriented concept, we know that all objects are described by type, but the contrary is not the case. Not all categories are used to describe the object, if a class does not contain enough information to describe a specific target, such a class is an abstract class. An abstract class is often used to our characterization of the problem areas in the analysis, design drawn abstraction is a series of looks different, but essentially the same concept of the abstract concrete. For example: If we have a graphics editing software development, we will find that there are areas of circular, triangular this specific concept, they are different, but they also belong to the shape of such a concept, the concept of shape is not in the problem areas exist, it is an abstract concept. It is precisely because the concept of abstract no counterpart in the field of the specific concept, used to describe abstract concept of an abstract class can not be the case.
In object-oriented fields, mainly used for abstract class type hide. We can construct a fixed behavior of a group of abstract description, but this group is able to conduct arbitrary possible to achieve the specific way. This is the abstract description of the abstract category, and this group of arbitrary possible a concrete realization of the performance for all possible derived class. Modules can operate in an abstract. Because the module relies on the abstract of a fixed, it can be modified; At the same time, from the abstract of this derivative, may also expand this module function. Readers familiar with the OCP will be aware that in order to achieve the object-oriented design one of the most core principles OCP (Open-Closed Principle), an abstract class is one of the key.
Syntax definition from the perspective abstract class and interface
In the syntax level, the Java language interface and the abstract class is given a different definition, a definition below to the abstract class called Demo as an example to illustrate the difference.
Use the abstract class defined Demo abstract class in the following manner:
(Abstract class Demo
Abstract void method1 ();
Abstract void method2 ();
…
)
The use interface Demo abstract class defined in the following manner:
(Interface Demo
Void method1 ();
Void method2 ();
…
)
In the abstract class methods, Demo can have their own data members can be members of the non-abstarct approach, and the way in the implementation of the interface, Demo can only have static data can not be amended members (that is, must be static final , but generally not in the interface definition data members), all members of the methods are abstract. In a sense, the interface is a special form of abstract class.
From the point of view of programming, the abstract class and the interface can be used to achieve the "design by contract" thinking. However, in the use of concrete above or are there some distinction.
First, the abstract class in the Java language that is an inheritance, a class can only be used once inheritance. However, a class can implement multiple interface. Perhaps this is the designers of the Java language Java in considering support for multiple inheritance of a compromise to consider it.
Secondly, the definition of the abstract class, we can give the default behavior method. However, in the definition of interface, but not with the default method, in order to circumvent this limitation, you must use commissioned, but it will be some additional complexity, and sometimes a lot of trouble.
In the abstract class can not be defined in the default behavior of still another more serious issue, which is likely to cause trouble maintaining. If subsequently want to change the interface type (usually through abstract class or interface to express) to adapt to the new situation (for example, add a new method or methods have been used to add new parameters), will be very troublesome, may have to spend a lot of time (for many of the derived class, it is particularly the case). However, if the interface is achieved through abstract class, then may only need to amend the definition of the abstract class in the default behavior on it.
Similarly, if not in the abstract class defined in the default behavior, it will lead to the same method appears in the abstract category in each derived class, in violation of the "one rule, one place" principle, resulting in duplication of code, and are not conducive to the future maintenance. Therefore, in the abstract class and the interface between the choices to be very cautious.
Design Concept perspective from the abstract class and interface
Mainly from the above definition of syntax and programming perspective on the interface of the abstract class and distinction, the difference between these levels are relatively low level, the Feibenzhi. This section will be another level: abstract class and interface design reflects the idea to an analysis of the difference between the two. The writer believes that, from this level of the two can understand the essence of the concept.
As already mentioned, abstarct class in the Java language reflects a succession relations, in order to make reasonable inheritance, and the father of derived class must exist between "is a" relationship, that is, and the father of the concept of nature derived class should be the same (references in [3] on "is a" relationship of the length of the in-depth exposition, interested readers can reference). , The interface is not, does not require interface and the realization of the concept of interface definition is essentially the same, only the realization of the interface definition of lease it. In order to facilitate understanding discussed below will be through a simple example of a description.
Consider this an example, we assume that the problem areas, there is a Door on the abstract concept, with the implementation of the Door open and close the two movements, at this time we can abstract class or interface to the definition of an abstract concept that the type of definition of ways were as follows:
Use abstract class defined Door:
Abstract class Door (
Abstract void open ();
Abstract void close ();
)
Use interface defined Door:
(Interface Door
Void open ();
Void close ();
)
Other specific types Door extends the use of abstract class can be defined using the Door or implements the interface defined Door. Looks like interface and the use of abstract class does not have major differences.
If the request has to Door Alarm function. How are we going to design for the structure of the examples of this category (in this case, is to display abstract class and interface design concept reflected in the difference between the other issues unrelated to have done to simplify or ignored)? Luo listed below will be possible solutions, and design the conceptual level from these options for analysis.
Solution 1:
Door in the simple definition of an additional alarm, as follows:
Abstract class Door (
Abstract void open ();
Abstract void close ();
Abstract void alarm ();
)
Or
(Interface Door
Void open ();
Void close ();
Void alarm ();
)
So with alarm function AlarmDoor the definition as follows:
(Class AlarmDoor extends Door
Void open () ()…
Void close () ()…
Void alarm () ()…
)
Or
(Class AlarmDoor implements Door
Void open () ()…
Void close () ()…
Void alarm () ()…
)
This measure violates the object-oriented design of a core principle ISP (Interface Segregation Priciple), Door to Door in the definition of the concept of inherent behavior methods and another concept "alarm" act together in the mixed method. This is a problem caused by those who rely solely on the concept of the Door Module because of the "alarm" the concept of this change (for example: Amending alarm method parameters) changed, still the contrary.
Solution 2:
Since the open, close and alarm are two different concepts, according to the principle of ISP should be representative of their respective definitions of these two concepts in the abstract category. Definition of ways: the use of these two concepts are abstract class defined; two concepts are defined to use interface and an abstract class concept of the use of defined, and the other concept of the use of defined interface.
Clearly, the Java language does not support multiple inheritance, the two concepts are defined using abstract class is not feasible. Behind both are feasible, but it is their choice in the field of the problem reflects the essence of the concept of understanding, designed to reflect the correct and rational. We January 1 to analyze that.
If the two concepts are used to define the interface mode, then reflects two problems: 1, we may not have a clear understanding of the field, AlarmDoor the concept is essentially in the end Door or alarm? 2, if we have no understanding of problem areas, such as: We have, through the analysis of problem areas found in the concept of nature AlarmDoor and Door is the same, so when we realize there is no right to expose our design intent, because In both the definition of the concept (use interface are defined) do not reflect the meaning.
If we understand the problem areas are: AlarmDoor the concept is essentially Door, at the same time it is the function of the police. How are we going to design and realize that we have to clear the meaning? Said earlier, the abstract class in the Java language that a succession, and inheritance, in essence, "is a" relationship. Therefore Door concept, we should use abstarct class approach to the definition. In addition, the alarm function is also AlarmDoor that it can complete the definition of the concept of the report, so the concept of alarm through the interface can be defined. As follows:
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 basically means to realize that we have a clear understanding of the problem areas, and correctly expose our design intent. In fact, that is the abstract class "is a" relationship, and that the interface is "like a" relationship, we can choose as a basis, of course, because it is based on the understanding of the problem areas, such as: If we think that AlarmDoor the concept is essentially a warning, a Door at the same time, the function, then the above definition of a way to turn.
</ Td> </ tr> <tr>
↑ Back
Tags: Classes






