1. Abstract class:
Abstract classes are special classes, but they cannot be instantiated; in addition, they have other characteristics of classes; what is important is that abstract classes can include abstract methods, which ordinary classes cannot. Abstract methods can only be declared in abstract classes and do not contain any implementations, and derived classes must override them. In addition, abstract classes can be derived from an abstract class, and abstract methods of base classes can be overwritten or not. If not overwritten, their derived classes must overwritten them.
2. Interface:
Interfaces are reference types, similar to classes, and have three similarities with abstract classes:
1. Cannot be instantiated;
2. Contains unimplemented method declaration;
3. The derived class must implement unimplemented methods. Abstract classes are abstract methods, and interfaces are all members (not only methods, including other members);
In addition, the interface has the following characteristics:
In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as public. Apart from this, no other members can be included, such as: constants, domains, constructors, destructors, and static members. A class can directly inherit multiple interfaces, but can only directly inherit one class (including abstract classes).
3. The difference between abstract classes and interfaces:
1. Classes are abstractions of objects. Abstract classes can be understood as treating classes as objects. The abstract class is called abstract classes. And interfaces are just a norm or regulation of behavior. Microsoft's custom interfaces always come with a field afterwards, proving that they are expressing a class of classes "I can do it...". Abstract classes are more defined between a series of closely related classes, while most interfaces are classes with loose relationships but all implement a certain function.
2. The interface basically does not have any specific characteristics of inheritance, it only promises methods that can be called;
3. A class can implement several interfaces at a time, but can only extend one parent class.
4. Interfaces can be used to support callbacks, but inheritance does not have this feature.
5. Abstract classes cannot be sealed.
6. The specific methods implemented by abstract class are default to virtual, but the interface methods in the class that implements interfaces are default to non-virtual, of course you can also declare them as virtual.
7. (Interface) Similar to non-abstract classes, abstract classes must also provide its own implementation for all members of the interface listed in the base class list of that class. However, abstract classes are allowed to map interface methods to abstract methods.
8. Abstract classes implement a principle in OOP, separating mutable and immutable. Abstract classes and interfaces are defined as immutable, and the variable seat subclass is implemented.
9. A good interface definition should be specific and functional, rather than multifunctional, otherwise it will cause interface contamination. If a class only implements one of the functions of this interface and has to implement other methods in the interface, it is called interface pollution.
10. Try to avoid using inheritance to achieve the assembly function, but use black box reuse, that is, object combination. Because the inheritance levels increase, the most direct consequence is that when you call a certain category in this group, you must load them all into the stack! The consequences can be imagined. (understanding in combination with the stack principle). At the same time, friends who are interested can notice that Microsoft often uses the method of object combination when building a class. For example, the Page class has properties such as Server Request, but in fact they are all objects of a certain class. Using this object of the Page class to call the methods and properties of another class is a very basic design principle.
11. If an abstract class implements an interface, the methods in the interface can be mapped into the abstract class as abstract methods without having to be implemented, and methods in the interface can be implemented in the subclass of the abstract class.
4. Use of abstract classes and interfaces:
1. If you are expected to create multiple versions of the component, create an abstract class. Abstract classes provide simple ways to control component versions.
2. If the created function will be used between a large range of different objects, use the interface. If you want to design small and concise function blocks, use the interface.
3. If you want to design large functional units, use abstract classes. If you want to provide common implemented functions among all implementations of the component, use abstract classes.
4. Abstract classes are mainly used for closely related objects; while interfaces are suitable for providing general functions for unrelated classes.
The following are a few image metaphors I saw on the Internet. They are really good, haha:
1. Airplane flying, birds can fly, and they all inherit the same interface "flying"; but F22 belongs to the aircraft abstract category, and pigeons belong to the bird abstract category.
2. Just like iron doors and wooden doors are all doors (abstract class), I can’t give you a door if you want (can’t be instantiated), but I can give you a specific iron door or wooden door (polymorphism); and it can only be a door, you can’t say it is a window (single inheritance); a door can have a lock (interface) or a doorbell (multiple implementation). The gate (abstract class) defines what you are, and the interface (lock) specifies what you can do (it is best for an interface to do only one thing, you cannot ask the lock to make sounds (interface pollution)).