SoFunction
Updated on 2025-03-09

A brief analysis of the difference between interfaces and classes in .NET

Preface

Everyone should know that interfaces are provided in .Net, which is different from the type definition of Class or Struct. Interfaces may seem like abstract classes, so some people think that in .Net, the abstract classes can be replaced entirely with interfaces. In fact, interfaces and abstract classes have their own strengths and weaknesses, so in applications, the two should be 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 together.

Next, let’s talk about the difference between abstract classes and interfaces:

The difference is that the two express different concepts. Abstract classes are a high degree of aggregation of a type of things. Therefore, for subclasses that inherit abstract classes, they belong to the "yes" relationship for abstract classes; while interfaces define behavioral norms, so for subclasses that implement interfaces, compared with interfaces, "behavior needs to be completed according to interfaces." These sound a bit false, for example. For example, dogs are a general term for all dogs. Jingha is a dog and sheepdog is a dog. Then the general characteristics of dogs will be found in Jingha and sheepdogs. Then, compared with Jingha and Sheepdogs, dogs belong to the abstract type of this type of thing; and for the action of "barking", dogs can bark and birds can bark. It is obvious that the former is equivalent to what is mentioned as an abstract class, while the latter refers to an interface.

Difference 2: When an abstract class defines a type method, it can give the implementation part of the method or not; for an interface, the defined methods cannot give the implementation part.

For example:

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

Difference 3: Inheritance classes are different in the implementation of the methods involved in the two. The inherited class does not need to be overridden for abstract methods defined by abstract classes, that is, the abstract class methods can be used; and for the methods or attributes defined by interface classes, corresponding methods and attribute implementations must be given in the inherited class.

Difference 4: If a new method is added to the abstract class, the inheritance class can be used for no processing; for the interface, the inheritance class needs to be modified to provide a newly defined method.

After knowing the difference between the two, let’s talk about the advantages of interfaces over abstract classes.

Benefit 1: The interface can not only act on the reference type, but also on the value type. In terms of abstract classes, they can only act on reference types.

Benefit 2: .Net type inheritance can only be single inheritance, which means that a type can only inherit one type and can inherit multiple interfaces. In fact, I agree with this point, more inheritance will make the inheritance tree confusing.

Benefit 3. Since an interface only defines properties and methods and has no much relationship with the type that is truly implemented, the interface can be reused by multiple types. Compared with this, the relationship between abstract classes and inherited classes is closer.

Advantage 4: Through interfaces, the properties and methods of type exposure can be reduced, thereby facilitating the protection of type objects. When an implementing interface type may contain other methods or attributes, but when the method returns, it can return the interface object. In this way, the caller can only access the relevant elements of the object through the methods or attributes provided by the interface, so that other elements of the object can be effectively protected.

Benefit 5: Decreasing value type unboxing operation. For the value type data defined by Struct, when stored in the collection, whenever it is taken out, an unboxing operation is required. At this time, a combination of Struct+Interface is used to reduce the unboxing operation.

Compared with abstract classes, interfaces have so many benefits, but interfaces have a fatal weakness, that is, the methods and attributes defined by interfaces can only be compared with the types that inherit it (unless the function labels defined by the interface are modified in the inheritance class). Therefore, when using multiple-layer inheritance relationships, it is difficult to implement them by using interfaces alone. Because if each type is allowed to inherit the interface and implement it, first of all, not to mention that writing code is more cumbersome, sometimes the execution result is still wrong, especially when the subtype object is implicitly converted into a base class object for access.

Then at this time, it is necessary to use interfaces combined with virtual methods to achieve it. In fact, in inheritance, whether to use interfaces or abstract classes. The interface is fixed and conventional, so the implementation of the corresponding methods and attributes of the interface must be provided in the inheritance class. For abstract classes, the implementation of the definition method of abstract classes runs through the entire inheritance tree, so the implementation or rewriting of the methods is uncertain. Therefore, relatively speaking, abstract analogy is more flexible than interfaces.

A simple comparison table of the two is given below.

interface

Abstract Class

More inheritance

support

Not supported

Type Limitations

No

Yes, only reference types

Method implementation

Method implementation must be given in inheritance types

Inheritance class can not be given

Extensibility

More troublesome

Relatively flexible

Multi-layer inheritance

It's more troublesome, need to use virtual functions

More flexible

In general, interfaces and abstract classes are language means provided by .Net to better implement inheritance relationships between types, and the two are somewhat complementary. Therefore, I do not emphasize what to use instead of what to use. The key to the problem is that how to reasonably apply these two means to the program is crucial.

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.