Analysis on the Difference between Interface and Class in. NET

  • 2021-10-11 18:06:50
  • OfStack

Preface

As you should know, interfaces are provided in. Net, which is different from the type definition of Class or Struct. Interfaces look like abstract class 1 in some cases, so some people think that abstract classes can be completely replaced by interfaces in. Net. In fact, interfaces and abstract classes have their own strengths and weaknesses, so they are often used in combination to complement each other's strengths and weaknesses. Let's not say much below. Let's take a look at the detailed introduction.

Let's talk about the difference between abstract classes and interfaces:

Difference 1. The concepts expressed by the two are different. Abstract class is a highly aggregated class of things, so for subclasses that inherit abstract classes, for abstract classes, it belongs to the relationship of "yes"; Interfaces define behavior specifications, so for subclasses that implement interfaces, compared with interfaces, it is "behavior needs to be completed according to interfaces". These sound a little empty, for example. For example, dogs are a general name for all dogs, Jingha is a dog, and shepherd dogs are dogs, so one characteristic of dogs will be found in Jingha and shepherd dogs, so dogs belong to the abstract type of such things compared with Jingha and shepherd dogs; For the action of "barking", dogs can bark and birds can bark. Obviously, the former is equivalent to abstract classes, while the latter refers to interfaces.

Difference 2. When an abstract class defines a type method, it can give the implementation part of the method or not; For interfaces, none of the methods defined in them can give the implementation part.

For example:


publicabstractclassAbsTest
{
publicvirtualvoidTest()
{
Debug.WriteLine("Test");
}
publicabstractvoidNewTest();
}
publicinterfaceITest
{
voidTest();
voidNewTest();
}

Difference 3. Inheriting classes have different implementations of the methods involved in the two. Inheriting classes can not override the abstract methods defined by abstract classes, that is to say, they can extend the methods of abstract classes; For the methods or attributes defined by the interface class, the corresponding implementation of the methods and attributes must be given in the inherited class.

Difference 4, in the abstract class, if you add a method, you can not use it as any processing in the inherited class; For interfaces, it is necessary to modify the inherited class and provide newly defined methods.

Knowing the difference between the two, let's talk about the advantages of interfaces over abstract classes.

Benefit 1. Interfaces can be used not only for reference types, but also for value types. For abstract classes, they can only work on reference types.

Benefit 2. The type inheritance of. Net can only be single inheritance, that is to say, a type can only inherit one type, but can inherit multiple interfaces. In fact, I also agree with this point. Multi-inheritance will make the inheritance tree chaotic.

Benefit 3. Interfaces can be reused by multiple types because they only define properties and methods and do not have much to do with the type that is actually implemented. In contrast, abstract classes are more closely related to inherited classes.

Benefit 4. Through interfaces, you can reduce the attributes and methods exposed by types, thus facilitating the protection of type objects. When a type implementing an interface may contain other methods or attributes, but when the method returns, it can return the interface object, so that the caller can only access the related elements of the object through the methods or attributes provided by the interface, which can effectively protect other elements of the object.

Benefit 5. Reduce unpacking operations of value types. For the value type data defined by Struct, when it is stored in the collection, it needs to be unpacked whenever it is taken out. At this time, the method of combining Struct+Interface is adopted to reduce the unpacking operation.

Compared with abstract classes, interfaces have so many advantages, but interfaces have one fatal weakness, that is, the methods and attributes defined by interfaces can only be relative to the types that inherit them (unless the function labels defined by interfaces are modified in the inherited classes), so it is difficult to implement them with interfaces alone when there are multiple inheritance relationships. Because if each type inherits the interface and implements it, first of all, it is tedious to write code, and sometimes the result of execution is still wrong, especially when subtype objects are implicitly converted into base class objects for access.

Then, at this time, it needs to be realized by combining interfaces with virtual methods. In fact, in inheritance, whether to use interfaces or abstract classes. Interfaces are fixed and conventional, so the implementation of the corresponding methods and properties of the interface must be provided in the inherited class. For abstract classes, the definition of abstract classes and the implementation of methods run through the whole inheritance tree, so the implementation or rewriting of methods is uncertain. Therefore, abstract analogy is relatively more flexible than interface.

A simple comparison table is given below.

接口

抽象类

多继承

支持

不支持

类型限制

没有

有,只能是引用类型

方法实现

继承类型中必须给出方法实现

继承类中可以不给出

扩展性

比较麻烦

相对比较灵活

多层继承

比较麻烦,需要借助虚函数

比较灵活

Generally speaking, interfaces and abstract classes are the language means provided by. Net to better realize the inheritance relationship between types, and they are somewhat complementary. So I don't emphasize what to use and what not to use, so the crux of the problem lies in how to apply these two means to the program reasonably, which is very important.

Summarize


Related articles: