Differences between abstract classes and interfaces
Abstract classes can contain non-abstract methods, and interfaces can only contain abstract methods.
Abstract classes cannot contain braces when declaring abstract methods, but all methods in the interface do not.
Abstract class ):
A. abstract classes are only for derivation. An abstract class cannot be instantiated. Only classes derived from it can be instantiated.
B. The main feature of an abstract class is that it contains abstract members. Abstract members are non-implemented methods or attributes, and their role is to force all derived classes to provide implementations.
C. Since abstract members should be overwritten, such members will automatically become virtual members and cannot be declared in this way.
D. Abstract members cannot be private; otherwise, they cannot be seen in the derived class.
Interface ):
A. The interface is a variant of the abstract class.
B. A key feature of an interface is that it does not contain implementation or data.
C. Fields (that is, data) cannot appear in an interface. If an interface requires a derived class to contain specific data, it uses attributes instead of fields. Because the attribute does not contain any implementation as part of the interface declaration, it does not reference a supported field.
D. The purpose of an interface is to define a contract to be followed by multiple classes. Therefore, all the members of the interface must be of the Public type.
Abstract classes do not need to implement all methods of interfaces.
Sometimes the interface and abstract class need to be used together, which can provide developers with considerable convenience and developers can choose which one is convenient. Such abstract classes are called convenience classes. In this case, the convenience class does not need to implement all methods of the interface, but can be left to the subclass that inherits it to implement them.
This is not meaningless. When you write a class that wants to use some methods in the interface (note that not all methods ), then you can use an abstract class to implement this interface first (the method body is empty), and then use your class to inherit this abstract class, so that you can achieve your goal, if you directly use classes to implement interfaces, all methods must be implemented.