|
Deep understanding of abstract class and interface
Abstract class and interface are two mechanisms for supporting abstract class definitions in the Java language. It is precisely because of the existence of these two mechanisms that Java has a strong object-oriented capability. Abstract class and interface have great similarities in their support for abstract class definitions, and they can even replace each other. Therefore, many developers seem to be more casual in choosing abstract class and interface when defining abstract classes. In fact, there is still a big difference between the two. Their choice even reflects the understanding of the nature of the problem domain and whether the understanding of the design intent is correct and reasonable. This article will analyze the differences between them, trying to provide developers with a basis for choosing between the two.
Understand abstract classes
Abstract class and interface are both used for 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 used to define abstract classes in the Java language One way, readers should pay attention to the distinction), then what is an abstract class, and what benefits can using abstract classes bring us?
In object-oriented concepts, we know that all objects are described by classes, but the reverse is not true. 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 characterize the abstract concepts we get in the analysis and design of the problem domain. It is an abstraction 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 circles and triangles in the problem domain. They are different, but they all belong to the concept of shape. The concept of shape is not in the problem domain. Existing, it is an abstract concept. It is precisely because abstract concepts have no corresponding specific concepts in the problem domain, so abstract classes used to represent abstract concepts cannot be instantiated.
In the object-oriented field, 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 have any number of possible concrete implementations. This abstract description is an abstract class, and this set of any possible concrete realizations is represented as all possible derived classes. The module can manipulate an abstract body. Because the module depends on a fixed abstract body, it can not be modified; at the same time, by deriving from this abstract body, the behavior and functions of this module can also be extended. Readers familiar with OCP must know that in order to be able to realize one of the most core principles of object-oriented design, OCP (Open-Closed Principle), abstract classes are the key.
Look at abstract class and interface from the grammar definition level
At the grammatical level, the Java language gives different definitions for abstract class and interface. The following takes the definition of an abstract class named Demo as an example to illustrate this difference. |
|