Details of abstract classes and interfaces in c

  • 2020-05-19 05:39:04
  • OfStack

1. Abstract class:
Abstract classes are special classes that simply cannot be instantiated; In addition, it has other properties of the class. The important thing is that abstract classes can include abstract methods, which ordinary classes can't. Abstract methods can only be declared in abstract classes and do not contain any implementations, which derived classes must override. In addition, an abstract class can derive from an abstract class, and the abstract methods of a base class can be overridden or unoverridden, if not, their derived classes must overwrite them.

2. The interface:
An interface is a reference type, similar to a class, and similar to an abstract class in three ways:
1. Cannot instantiate;
2. Contains unimplemented method declarations;
3. Derived classes must implement unimplemented methods, abstract classes are abstract methods, and interfaces are all members (not only methods but other members);

In addition, the interface has the following features:
Interfaces can contain properties, indexers, events, as well as methods, and these members are all defined as common. You must not include any other members, such as constants, fields, constructors, destructors, or static members. A class can directly inherit multiple interfaces, but only one class (including abstract classes) can be directly inherited.

3. Difference between abstract class and interface:
Class is the abstraction of the object, which can be understood as taking the class as an object, and the abstract class is called abstract class. The interface is only a behavior specification or regulation, and Microsoft's custom interface is always followed by able field, proving that it is the expression of class 1 class "I can do...". Abstract classes are more often defined between closely related classes in series 1, while interfaces are mostly classes with loose relationships that all implement a function.
2. The interface basically does not have any specific characteristics of inheritance, it only promises methods that can be called;
3.1 classes can implement several interfaces at a time, but only one parent class can be extended
4. Interfaces can be used to support callbacks, which inheritance does not.
5. Abstract classes cannot be sealed.
6. The concrete methods of the abstract class implementation are virtual by default, but the interface methods in the class that implements the interface are non-virtual by default, or you can declare them virtual.
7. (interface) similar to a non-abstract class, an abstract class must provide its own implementation for all members of the interface listed in the base class list of that class. However, an abstract class is allowed to map interface methods to abstract methods.
8. Abstract classes implement one of the principles in oop, separating the mutable from the immutable. Abstract classes and interfaces are defined as immutable, and mutable seat subclasses are implemented.
9. A good interface definition should be one that is functional, not multifunctional, otherwise it will pollute the interface. If a class implements only one of the functions of the interface and has to implement other methods in the interface, it is called interface pollution.
10. Avoid using inheritance to implement component functionality, and instead use black-box reuse, or object composition. Because the hierarchy of inheritance increases, the most immediate consequence is that when you call a class 1 in this class group, you have to load them all onto the stack! The consequence can be imagined. At the same time, those of you who are interested will notice that Microsoft USES a lot of object composition when building a class. For example, in asp.net, the Page class has properties like Server Request, but they are all objects of a class. Using this object of the Page class to invoke 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 to the abstract class as abstract methods without implementation, and the methods in the interface can be implemented in subclasses of the abstract class.

4. Use of abstract classes and interfaces:
1. If you expect to create multiple versions of the component, create an abstract class. Abstract classes provide simple ways to control component versioning.
2. Use interfaces if the functionality you create will be used between a wide range of disparate objects. If you want to design small, concise chunks of functionality, use interfaces.
If you want to design large units of functionality, use abstract classes. If you want to provide common implemented functionality across all implementations of a component, use abstract classes.
4. Abstract classes are mainly used for closely related objects; Interfaces are suitable for providing generic functionality for unrelated classes.


Here are a few imagery metaphors I've seen online that are really good, hehe:
1. Airplanes can fly, birds can fly, they all inherit the same interface "fly"; But F22 belongs to the abstract class of airplanes, and pigeons belong to the abstract class of birds.
2. Just like iron door and wood door are all doors (abstract class), I can't give you a door if you want (can't instantiate), but I can give you a concrete iron door or wood door (polymorphic); And it can only be a door. You can't call it a window. A door can have either a lock (interface) or a doorbell (multiple implementation). A door (abstract class) defines what you are, and an interface (lock) defines what you can do (an interface should only do one thing, you can't ask for a lock to make a sound (interface pollution)).


Related articles: