Analyze the differences between abstract classes and interfaces in Java in detail
In the Java language, abstract class and interface are two mechanisms that support abstract class definition. It is precisely because of the existence of these two mechanisms that Java is endowed with powerful object-oriented capabilities. 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.
Understanding abstract classes
Abstract class and interface are abstract classes in the Java language (the abstract class in this article 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). So what is an abstract class and 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 in 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 some specific concepts such as circle and triangle in the problem field. They 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.
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: