In the Java language, abstract class and abstract class interface is to support the definition of two mechanisms. It is because of the existence of these two kinds of mechanisms, it gives a powerful object-oriented Java ability. between abstract class and interface defined in the abstract class for the support of great similarity, or even to replace each other, so a lot of developers in the definition of abstract class abstract class and interface for the choice is more arbitrary. In fact, there are still between the two very different, and even for their choice reflects the nature of the problem areas of understanding, the understanding of the design intent is correct, reasonable. In this paper, the distinction between them will carry out some analysis to try to provide a developer to choose between the two basis.
Understanding of abstract class
abstract class and interface in the Java language are used to abstract class (in this article from the abstract class is not a translation from the abstract class, it is expressed in an abstract body, and the abstract class for the Java language is used to define the abstract class a way, please note the distinction between readers) the definition, then what is abstract class, the use of abstract class can bring us any good?
In the concept of object-oriented, we know that all the objects are described through the categories, but this is not the case in turn. Not all categories are used to describe the object, if a category does not contain sufficient information to describe a specific object, so that the class is abstract class. Abstract class is often used for characterization of the problem in our area of analysis, design abstraction to draw is a series of different looks, but in essence the same concept of the abstract concrete. For example: If we have a graphics editing software, you will find there is a circular problem areas, such as a specific concept of the triangle, they are different, but they also belong to the concept of such a shape, the shape of the concept is not in the problem areas exist, it is an abstract concept. It is precisely because the concept of an abstract does not correspond to problem areas in the concrete concept and abstract concept for the characterization of the abstract class can not be instantiated.
Object-oriented in the field of abstract class type is mainly used to hide. We can construct a fixed set of abstract description of behavior, but behavior of this group is likely to have any specific implementations. This abstract class is an abstract description, and this group of any possible concrete realization of the possible performance for all derived classes. Modules can operate an abstract body. As the module relies on a fixed body of the abstract, it can be modified is not allowed; At the same time, through the body derived from the abstract, but also the expansion of this function module. Readers familiar with the OCP must be aware that in order to achieve an object-oriented design principles of the core OCP (Open-Closed Principle), abstract class is one of the key.
Level from the syntax definition of abstract class and interface
Level in the syntax, Java language for the abstract class and interface definition is given a different way, following the definition of a Demo in the abstract class as an example to illustrate this difference.
The use of the abstract class abstract class defined manner Demo are as follows:
abstract class Demo (
abstract void method1 ();
abstract void method2 ();
…
)
Defined using the interface of the abstract class Demo as follows:
interface Demo (
void method1 ();
void method2 ();
…
)
Abstract class in the way, Demo can have their own data members, there can be a member of the non-abstract methods, and in the interface mode of implementation, Demo can only have static data can not be a member to amend (that is, must be static final , but generally not in the interface in the definition of data members), all members of the 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 achieve the “design by contract” thinking. However, the use of concrete above there are some differences.
First of all, abstract class in the Java language that is an inheritance, a type of inheritance can be used only once (because Java does not support multiple inheritance – to note). However, it can be a class of multiple interface. Perhaps this is a Java language for designers to consider Java support multiple inheritance of a compromise to consider it.
Secondly, the definition of abstract class, we can give way to the default behavior. However, in the definition of interface, the method does not have a default behavior, in order to circumvent this restriction, the commission must be used, but this will increase the number of complex and sometimes cause great trouble.
Abstract class in the definition of the default behavior is not there another more serious problem, and that is likely to cause maintenance problems. If you later want to change because the type of interface (usually through the abstract class or interface to that) in order to adapt to new circumstances (for example, to add a new method or methods have been used to add new parameters) when the trouble is, may have to spend a lot of the time (for the derived class a lot of cases, particularly). However, if the interface is achieved through the abstract class, you may only need to modify the definition of abstract class in the default behavior in it.
Similarly, if not in the abstract class defined in the default behavior, it will lead to the same method appear in the abstract class for each derived class, the violation of “one rule, one place” principle, resulting in code duplication, the same is not conducive to the future maintenance. As a result, between abstract class and interface to choose very carefully.
Level from the design concept of abstract class and interface
Above definition of the main syntax and programming from the perspective of the abstract class and interface difference is the difference between these levels is relatively low-level, unessential. This subsection will be on another level: abstract class and interface design reflects the idea to analyze the difference between the two. The author believes that an analysis from the level they can understand the essence of the concept.
As already mentioned, abstract class in the Java language embodies a succession of relationships, in order to make a reasonable inheritance, the parent class and derived class must exist between the “is-a” relationship, that is the parent class and derived class in the concept of should be essentially the same. For example interface is not, does not require the realization of interface and interface definition of the concept is essentially the same, only the interface definition of a lease only. In order to facilitate the understanding of exposition, the following will be through a simple example to explain.
Consideration of such an example, assume that the problem in our field on the Door of an abstract concept, the Door with the implementation of the two movements open and close, at this time we can abstract class or interface to define a type that the abstract, respectively defined as follows:
Abstract class defined by the use of Door:
abstract class Door (
abstract void open ();
abstract void close ();
)
Interface defined by the use of Door:
interface Door (
void open ();
void close ();
)
Door of other specific types extends the use of abstract class can be defined by the Door, or implements the use of the interface defined Door. It looked as if the use of abstract class and interface there are no major difference.
If you are asking Door Alarm also has a function. How are we going to design for the example of the structure of this class (in this case, is to display abstract class and interface design philosophy is reflected in the distinction, other aspects of the problem has nothing to do have done to simplify or ignore)? Below set out the possible solutions and ideas from the design dimensions of these different options for analysis.
Solution I:
Simple definition in the Door for an additional alarm methods, as follows:
abstract class Door (
abstract void open ();
abstract void close ();
abstract void alarm ();
)
Either
interface Door (
void open ();
void close ();
void alarm ();
)
Well, the police function with the definition of AlarmDoor as follows:
class AlarmDoor extends Door (
void open () (…)
void close () (…)
void alarm () (…)
)
Either
class AlarmDoor implements Door (
void open () (…)
void close () (…)
void alarm () (…)
)
This method violates the object-oriented design of a core principle of ISP (Interface Segregation Principle), in the definition of Door to Door inherent in the conduct of the concept and another concept “alarm” method of mixing the acts together. This problem is caused by those who only rely on the concept of Door module will “alarm” the concept of change (such as: modified method of alarm parameters) change, on the contrary remain.
Solution 2:
Since the open, close and the alarm are two different concepts, according to the principle of ISP they should be defined, respectively, on behalf of these two concepts in the abstract class. The definition of the way: the two concepts are defined using the abstract class; the two concepts are defined using the interface; a concept defined by the use of abstract class, and the other defined the concept of using the interface.
Obviously, due to Java language does not support multiple inheritance, so the two concepts are defined using the abstract class is not feasible. Behind the two methods are feasible, but their choice is reflected in the field for the problem of understanding the concept of nature, a reflection of the design intent is correct, reasonable. 11 to our analysis.
If the two concepts are used to define the interface methods, it reflects two problems: 1, we may not have a clear understanding of problem areas, AlarmDoor conceptual in nature or in the end is the Door alarm? 2, if we are to understand the issues there is no problem with the field, such as: We adopted the analysis of the problem areas found in the concept of the nature of AlarmDoor and Door is the same, then we have not been able to achieve when the right to reveal our design intent, as two concepts in this definition (using the interface defined) do not reflect the meaning of the above.
If we understand the problem areas are: AlarmDoor the concept of essence in Door, at the same time it has alarm function. How are we going to design and realize that we have come to a clear meaning? As already mentioned, abstract class in the Java language that a succession of relationships, and inheritance is, in essence, “is-a” relationship. Therefore, the concept of the Door, we should use the approach to the definition of abstarct class. In addition, AlarmDoor also have the alarm function on it to complete the definition of the concept of police conduct, so the concept of alarm can be defined through the 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 is basically the way to achieve that we have a clear understanding of the problem areas, the right to reveal our design intent. In fact, the abstract class that is “is-a” relationship, interface that is “like-a” relationship, we can choose as a basis, of course, this is based on the understanding of problem areas, such as: If we AlarmDoor concept that is essentially in alarm, but also has the function of Door, then the above definition of the way we should turn a.
Summary
1.abstract class in the Java language that is an inheritance, a type of inheritance can be used only once. However, it can be a class of multiple interface.
2. In the abstract class can have its own data members, they can have a non-member of methods abstarct, and in the interface, only to have static data can not be a member to amend (that is, must be static final, but in the interface generally do not define data members), all members of the methods are abstract.
3.abstract class and interface design reflects the idea is different. In fact, the abstract class that is “is-a” relationship, interface that is “like-a” relationship.
4. The realization of the abstract class and interface must implement all the methods. Abstract class can have non-abstract methods. Interface methods can not achieve.
5. The definition of interface variables are public static final default type, and must be addressed to its initial value, so can not be re-realization of the definition of class, not going to change its value.
6. Abstract class is the default variables in the friendly type, and its value can be redefined in subclasses, you can re-assignment.
7. The method of the default interface are public, abstract types.
Conclusion
abstract class and interface is the Java language in the definition of abstract class in two ways, they have a lot of similarity. However, their choice but the problem is often reflected in the field of the concept of the nature of understanding, a reflection of the design intent is correct, reasonable, and because they demonstrated the concept of the relationship between the different (although both can achieve the function of the demand). Actually, this is the kind of language usage, I hope readers realize a friend to detail.




No user commented in " Detailed Analysis of Java classes and interfaces in the abstract distinction "
Follow-up comment rss or Leave a TrackbackLeave A Reply