InterfaceIt combines public instance (non-static) methods and attributes to encapsulate a set of specific functions. The interface cannot exist independently and cannot be instantiated like an instantiated class. An interface cannot contain any code that implements its members, but can only define the members themselves. The implementation process must be completed in the class that implements the interface. In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).
Abstract classIt is a special class and cannot be instantiated. In addition, it has other characteristics of the class. abstract classes can include abstract methods or non-Abstract members. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them. Abstract class members can be private (as long as they are not abstract), protected, and internal (the protected internal members can only access the application code or derived classes ).
Differences between abstract classes and interfaces:
- Class is the abstraction of objects. abstract classes can be understood as classes as objects. abstract classes are called abstract classes. The interface is just a behavior specification or provision. Microsoft's custom interface always carries the able field to prove that it represents a class of "I can do it ...". Abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions.
- The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called.
- A class can inherit only one parent class, but multiple interfaces can be implemented.
- Interfaces can be used to support callback, but inheritance does not.
- Abstract classes cannot be sealed.
- The specific methods implemented by the abstract class are virtual by default, but the interface methods in the class implementing the interface are non-virtual by default. Of course, you can also declare them as virtual.
- Similar to a non-abstract class, an abstract class must also provide its own implementation for all the members of the interface listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods.
- Abstract classes implement a principle in OOP that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented.
- A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution.
- Avoid using inheritance to implement the build function, but use black box reuse, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined (based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example, in Asp.net, the page class has server request and other attributes, but in fact they are all objects of a certain class. This object of the page class is used to call the methods and attributes of other classes. This is a very basic design principle.
- If an abstract class implements an interface, you can map the methods in the interface to an abstract class without having to implement them. Instead, you can implement the methods in the abstract class.
Use of abstract classes and interfaces:
- If you want to create multiple versions of a component, create an abstract class. Abstract classes provide simple methods to control component versions.
- If the created function is used across a wide range of different objects, the interface is used. If you want to design small and concise functional blocks, use interfaces.
- If you want to design a large functional unit, use an abstract class. If you want to provide general implemented functions among all the implementations of the component, use an abstract class.
- Abstract classes are mainly used for closely related objects. interfaces are suitable for providing general functions for unrelated classes.
A simple comparison between the two is as follows:
| |
Interface |
Abstract class |
| Multi-Inheritance |
Supported |
Not Supported |
| Type restrictions |
No |
Yes. It can only be of the reference type. |
| Method implementation |
Method implementation must be provided in the inheritance type |
The inheritance class can be left empty. |
| Scalability |
Relatively troublesome |
Relatively flexible |
| Multi-layer inheritance |
It is troublesome. You need to use virtual functions. |
Flexible |