- Definition and difference of abstract class and interface?
- What problems do abstract classes solve in object-oriented programming?
- What problems does interface solve in object-oriented programming?
- How to decide whether to use abstract class or interface?
- What if you use a language that doesn’t support abstract classes and interfaces?
In object-oriented programming languages, most of them add support for abstract classes and interfaces, such as the most commonly used languages such as Java, C ා.
The above is the definition of an ordinary abstract class. How to use it? Du Niang has a lot of results. In fact, in general, the abstract class has the following characteristics:
- Abstract classes cannot be instantiated, they can only be inherited. That is to say, if new human() will report a compilation error
- Abstract class is also a class, which can contain properties and methods. Methods can contain implementation or not, which is called abstract method.
- Subclasses inherit abstract classes and must implement all the defined abstract methods, or the compiler will report compilation errors.
Abstract class is still a class in essence. It is just a special class that cannot be instantiated. However, it plays a very important role in the process of object-oriented design. In essence, abstract class reflects the relationship between is-a. just like the abstract class defined above, human type abstracts human. If I define a vegetable type to inherit this type
public class CaiCai : Human
Caicai class must provide an implementation of abstract methods before it can be compiled. Abstract class generation is an extension of object-oriented development idea, a solution to code reuse problem, and also a result of abstracting code. The design idea of abstract class is bottom-up, that is to say, there should be subclasses in the design. When the subclasses increase gradually, the common features can be abstracted to produce abstract classes.
Speaking of this, many students will ask, if I do not use abstract classes as a parent class can ah. Yes, ordinary classes can also take the place of abstract classes. But a few things seem strange
- The parent class can also be instantiated, but the methods to be abstracted seem strange, because these methods are only clearly defined in subclasses. For example, if the class human in the above code is changed to a normal type, what content should the method gender() return?
- During compilation, if the child class does not implement the method of the parent class, it will not report an error, which will increase the difficulty of troubleshooting. If there are many methods to be rewritten, the troubleshooting will be very headache
- If an abstract parent class can be instantiated, this is essentially against the idea of object-oriented. After all, a parent class is an abstract concept, and what it represents after being instantiated is more confusing
The most important role of interface in system design is decoupling. You should have heard the idea of “interface oriented programming” and dependency inversion more than once, which is also an embodiment of the idea of object-oriented design. An interface is essentially the behavior of an abstract object, or contract. In the interface oriented development, the caller does not care about the implementation of the interface, but depends on the definition of the interface. The stability of the interface definition represents the stability of a system. If there is a problem with the external interface definition of a system, the system will probably die.
public interface IHuman
The above is just a definition of a simple interface. The abstraction of an interface can be as small as an object’s behavior abstraction, as large as a service’s behavior abstraction, and more likely a system’s behavior abstraction. Therefore, interface is a very general concept, but it also reflects the object-oriented design concept in essence. Since interface is the definition of behavior, it has the following characteristics
- Interfaces can only define behaviors, and cannot contain implementations of behaviors
- When a type inherits an interface, it must implement all behaviors of the interface
- Interfaces are different from classes and cannot contain properties
Because the interface embodies the code of conduct, so the concept of object-oriented design can also be used in the definition of interface. When multiple different interfaces define the same behavior, we can consider abstracting higher-level interface to realize the reuse of behavior.
Write it at the end
Abstract class and interface are generated by the abstract behavior of objects, but the abstract class focuses more on the is-a relationship, which realizes code reuse, while the interface focuses more on the abstraction of behavior (has – a). To take a very simple chestnut, what should we do if we design an abstraction of birds? Different birds may have different colors of feathers. Such attributes can use abstract classes. Different birds may have different flight behaviors. Therefore, it is more appropriate to use interfaces to realize the abstraction of behavior classes.
Whether it is an interface or an abstract class, it reflects the relationship between the upper and lower levels in the code level. Even if a programming language does not provide the definition of the interface and abstract class, as long as the object hierarchy can be realized, object-oriented programming can be realized in principle. The abstract idea of programming is always around the top and bottom, and the internal and external dimensions are evolving reasonably.
When it comes to the definition of an interface, you can actually generalize it. There are only behavioral methods in the interface. In some programming languages that do not support interfaces, classes that only contain methods can be regarded as the abstract definition of interfaces. This makes sense in the design concept.
In the inheritance level and design process, abstract class is a bottom-up design idea. First there is the code of subclass, and when the number of subclasses increases, the parent class of higher level will be abstracted. Different from interfaces, interface oriented programming is a top-down design idea, which abstracts behavior contract first and then realizes it.
More wonderful articles
- Distributed large concurrency series
- Architecture design series
- Interest learning algorithm and data structure series
- Design pattern series