Detailed resolution of the differences between Java abstract classes and interfaces

  • 2020-04-01 01:26:41
  • OfStack

Abstractclass and interface are two of the mechanisms that support abstractclass definitions in the Java language, and it is because of these two mechanisms that Java has powerful object-oriented capabilities. Abstractclass and interface are very similar in terms of their support for abstractclass definitions, and can even be interchangeable, so many developers choose abstractclass and interface at will when defining an abstractclass. In fact, there is a big difference between the two, and the choice of them even reflects the understanding of the nature of the problem domain and whether the understanding of the design intention is correct and reasonable. This article will examine the differences between them in an attempt to give developers a basis for choosing between them.

Understanding abstract classes
Abstractclass and interface in the Java language is used for an abstract class (abstract class is not from the abstractclass translation in this article, it said is an abstract, but abstractclass for the Java language is used to define a method of abstract class, please pay attention to distinguish) defined, so what's an abstract class, using an abstract class can bring us any good?

In object-oriented concepts, we know that all objects are represented by classes, but not the other way around. Not all classes are intended to represent objects, and if a class does not contain enough information to represent a concrete object, such a class is an abstract class. Abstract classes are often used to represent the abstract concepts that we derive from our analysis and design of the problem domain. 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 some concrete concepts such as circle and triangle in the problem domain, they are different, but they all belong to the concept of shape, the concept of shape does not exist in the problem domain, it is an abstract concept. Because abstract concepts have no corresponding concrete concepts in the problem domain, abstract classes representing abstract concepts cannot be instantiated.

In the object-oriented world, abstract classes are mainly used for type hiding. We can construct an abstract description of a fixed set of behaviors, but this set of behaviors can be implemented in any concrete way possible. This abstract description is the abstract class, and this set of any possible concrete implementations is represented as all possible derived classes. A module can operate on an abstract body. Because a module depends on a fixed abstraction, it can be unmodifiable; At the same time, the behavior of this module can be extended by deriving from this abstraction. Readers familiar with OCP will know that abstract classes are key to one of the core principles of object-oriented design, the OCP open-closedprinciple.
Look at abstractclass and interface at the syntax definition level
At the syntax level, the Java language defines abstractclass and interface in different ways, and here's an example of how to define an abstractclass called Demo.

The way to define a Demo abstractclass using abstractclass is as follows:

 
abstractclassDemo {  
abstractvoidmethod1 (a);  
abstractvoidmethod2 (a);  
 ...  
 }  
 use interface Mode definition Demo The way to abstract a class is as follows:  
interfaceDemo{ 
voidmethod1 (a);  
voidmethod2 (a);  
 ...  
} 

In the abstractclass mode, Demo can have its own data members or non-abstarct member methods, whereas in the interface implementation, Demo can only have static data members that cannot be modified (that is, must be staticfinal, but data members are not generally defined in the interface), and all member methods are abstract. In a sense, interface is a special form of abstractclass.

From a programming perspective, both abstractclass and interface can be used to implement the idea of "designbycontract". But there are some differences in the specific use.
First, abstractclass represents an inheritance relationship in the Java language that can only be used once by a class. However, one class can implement multiple interfaces. Perhaps this was a compromise between the designers of the Java language and Java's support for multiple inheritance.

Second, in the definition of abstractclass, we can give methods their default behavior. However, in the definition of interface, methods cannot have default behavior, and in order to get around this limitation, delegates must be used, but this can add some complexity and sometimes cause a lot of trouble.
Another serious problem with not being able to define default behavior in an abstract class is that it can cause maintenance problems. Because if you later want to modify the interface of a class (typically represented by abstractclass or interface) to accommodate a new situation (for example, adding a new method or adding a new parameter to an already used method), it can be very cumbersome and can take a lot of time (especially if there are many derived classes). But if the interface is implemented with abstractclass, you might just need to modify the default behavior defined in abstractclass.

Similarly, failure to define default behavior in an abstract class results in the same method implementation appearing in every derived class of the abstract class, violating the "onerule, oneplace" principle, causing code duplication, which is also detrimental to future maintenance. Therefore, be very careful when selecting between abstractclass and interface.

Abstractclass and interface at the design philosophy level the distinction between abstractclass and interface is discussed above, mainly from the perspective of syntax definition and programming. This section examines the differences between abstractclass and interface at another level: the design philosophy reflected in abstractclass and interface. According to the author,


Related articles: