Summary of the differences between Java abstract classes and interfaces

Summary of the differences between Java abstract classes and interfaces

Abstract class and interface are two mechanisms to support abstract class definition in Java language. It is the existence of these two mechanisms that endow java with powerful object-oriented ability.

Abstract class and interface have great similarity in support of abstract class definition, and can even replace each other. Therefore, many developers choose abstract class and interface at will when defining abstract classes.

In fact, there are still great differences between the two. Their choice even reflects whether the understanding of the essence of the problem field and the understanding of the design intention are correct and reasonable. This paper will analyze the differences between them, trying to provide developers with a basis for choosing between them.

1、 Understanding abstract classes

Abstract class and interface are used to define abstract classes in the Java language (the abstract class in this paper is not translated from abstract class, it represents an abstract body, and abstract class is a method used to define abstract classes in the Java language, please pay attention to distinguish). What is an abstract class and use abstract classes

What benefits can it bring us?

In the concept of object-oriented, we know that all objects are described by classes, but the reverse is not the case. Not all classes are used to describe objects. If a class does not contain enough information to describe a specific object, such a class is an abstract class. Abstract classes are often used to represent the abstract concepts we get from the analysis and design of the problem field. They are abstractions of a series of concrete concepts that look different but are essentially the same. For example, if we develop a graphics editing software, we will find that there are specific concepts such as circle and triangle in the problem field, which are different, But they all belong to the concept of shape. The concept of shape does not exist in the problem field. It is an abstract concept. It is precisely because the abstract concept has no corresponding concrete concept in the problem field, the abstract class used to represent the abstract concept cannot be instantiated.

In the object-oriented domain, abstract classes are mainly used for type hiding. We can construct a fixed abstract description of a group of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and any set of possible concrete implementations are represented as all possible derived classes. A module can manipulate an abstraction. Because the module depends on a fixed abstract body, it can not be modified; At the same time, the behavior function of this module can also be extended by deriving from this abstract body. Readers familiar with OCP must know that in order to realize OCP (open closed principle), one of the core principles of object-oriented design, abstract classes are the key.

2、 On abstract class and interface from the perspective of syntax definition

At the syntax level, the Java language gives different definitions of abstract class and interface. The following takes defining an abstract class called demo as an example to illustrate this difference. The demo abstract class is defined using abstract class as follows:

The demo abstract class is defined using interface as follows:

In the abstract class mode, the demo can have its own data members or non Abstract member methods. In the implementation of the interface mode, the demo can only have static data members that cannot be modified (that is, it must be static final, but generally no data members are defined in the interface), and all member methods

They are abstract. In a sense, interface is a special form of abstract class. From the perspective of programming, abstract class and interface can be used to realize the idea of "design by contract". However, there are some differences in the specific use.

First, abstract class represents an inheritance relationship in the Java language. A class can only use the inheritance relationship once. However, a class can implement multiple interfaces. Perhaps this is a compromise between the designers of the Java language and Java's support for multiple inheritance.

Secondly, in the definition of abstract class, we can give the default behavior of methods. However, in the definition of interface, methods cannot have default behavior. In order to bypass this restriction, delegates must be used, but this will increase some complexity and sometimes cause great trouble. There is another serious problem that default behavior cannot be defined in abstract classes, that is, it may cause maintenance trouble. Because if you want to modify the class interface (generally represented by abstract class or interface) to adapt to new situations (for example, adding new methods or adding new parameters to used methods), it will be very troublesome and may take a lot of time (this is especially true for 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 implementation will appear in each derived class of the abstract class, violating the "one rule, one place" principle, causing code duplication, which is also not conducive to future maintenance. Therefore, you should be very careful when choosing between abstract class and interface.

3、 Abstract class and interface from the perspective of design concept

The above mainly discusses the differences between abstract class and interface from the perspective of syntax definition and programming. These differences are relatively low-level and non essential. This paper will analyze the difference between abstract class and interface from another level: the design concept reflected by abstract class and interface. The author believes that it is reasonable to analyze from this level

Understand the essence of the two concepts.

As mentioned earlier, Abstarct class embodies an inheritance relationship in the Java language. In order to make the inheritance relationship reasonable, there must be an "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 the same in essence. For the interface, it is not required that the implementer of the interface is consistent with the interface definition in concept, but only implements the contract of the interface definition. In order to make the discussion easy to understand, the following will be explained by a simple example.

Consider such an example. Suppose there is an abstract concept of door in our problem domain. The door has two actions open and close. At this time, we can define a type representing the abstract concept through abstract class or interface. The definition methods are as follows:

Define the door using abstract class method:

Define the door using the interface method:

For other specific door types, you can extend the door defined in the abstract class mode or implement the door defined in the interface mode. It seems that there is no big difference between using abstract class and interface.

If it is required now, door also has the function of alarm. How should we design the class structure for this example (in this example, it is mainly to show the difference between abstract class and interface reflected in the design concept, and other irrelevant problems have been simplified or ignored). The following will list possible solutions and analyze these different solutions from the design concept level.

Solution 1:

Simply add an alarm method to the definition of door, as follows:

perhaps

The alarmdoor with alarm function is defined as follows:

perhaps

This method violates a core principle ISP (interface aggregation priciple) in object-oriented design. In the definition of door, the behavior method inherent in the concept of door is mixed with the behavior method of another concept "alarm". One problem caused by this is that those modules that only rely on the concept of door will be "alarm" This concept changes (for example, modify the parameters of the alarm method), and vice versa.

Solution 2:

Since open, close and alarm belong to two different concepts, they should be defined in abstract classes representing these two concepts according to ISP principles. Definition methods: these two concepts are defined in abstract class; Both concepts are defined in interface mode; One concept is defined in abstract class mode, and the other concept is defined in interface mode.

Obviously, due to the Java language @ r_ 301_ 2431 @ multiple inheritance, so it is not feasible to use abstract class to define both concepts. The latter two methods are feasible, but their choice reflects the understanding of the essence of the concept in the problem field and whether the reflection of the design intention is correct and reasonable. Let's analyze and explain.

If both concepts are defined using the interface method, two problems are reflected:

1. We may not understand the problem area clearly. Is alarmdoor a door or an alarm in essence?

2. If there is no problem in our understanding of the problem domain, for example, through our analysis of the problem domain, we find that the concept of alarmdoor is consistent with that of door in essence, then we can not correctly reveal our design intention during implementation, because the above meanings cannot be reflected in the definitions of these two concepts (both defined by interface).

If our understanding of the problem field is that alarmdoor is essentially a door in concept, and it has the function of alarm. How can we design and implement to clearly reflect our meaning? As mentioned earlier, abstract class represents an inheritance relationship in the Java language, and the inheritance relationship is essentially an "is a" relationship.

Therefore, for the concept of door, we should use the Abstarct class method to define it. In addition, alarmdoor also has alarm function, which indicates that it can complete the behavior defined in the alarm concept, so the alarm concept can be defined through the interface. As follows:

This implementation can basically clearly reflect our understanding of the problem field and correctly reveal our design intention. In fact, abstract class represents the "is a" relationship, and interface represents the "like a" relationship, which can be used as a basis for your choice. Of course, this is based on the understanding of the problem field. For example, if we think that alarmdoor is essentially an alarm and has the function of door, the above definition method will be reversed.

Abstract class and interface are two ways to define abstract classes in the Java language, and they have great similarities. However, their choice often reflects the understanding of the essence of concepts in the problem field and whether the reflection of design intention is correct and reasonable, because they show different relationships between concepts (although they can realize the required functions). This is also a common usage of language.

Abstract methods are methods that must be implemented. Just like animals have to breathe. But fish breathe with gills and pigs breathe with lungs. Animals should have breathing methods. How to breathe is a subclass. Now there are many discussions and suggestions to advocate using interface instead of abstract class. In theory, the two can be mixed in general, but they are still different in practical application. Abstract classes are generally used as public parent classes to provide the basis for the extension of child classes. The extension here includes attribute and behavior. Generally speaking, interfaces do not consider attributes but only methods, so that subclasses can freely fill in or extend the methods defined by the interface. Just like the Java prince said, the adapter in events is a good application. Using a simple example, for example, a teacher, we regard it as an abstract class, which has its own attributes, such as age, education level, teacher number, etc., and teachers are divided into many types. We can inherit the teacher class and expand the unique category attribute, and the universal attribute has been directly inherited.

And the interface ~ take teachers as an example. Teachers have a lot of behaviors. In addition to being the same as ordinary people, they also have career related behaviors, such as changing examination papers, giving lectures, etc. we define these behaviors as methods without body. As a collection, it is an interface. The teachers' behavior characteristics are different, so they can expand their behavior body. In this sense, interface focuses on behavior.

In short, in many cases, interfaces can indeed replace abstract classes if you don't need to deliberately express inheritance on attributes.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>