Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between interfaces and classes in .NET?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about the differences between interfaces and classes in. NET. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Let's start with the difference between abstract classes and interfaces:

Difference 1: The concepts expressed by the two are different. Abstract classes are highly aggregated of a class of things, so for subclasses that inherit abstract classes, it belongs to the relationship of "yes" for abstract classes; while interfaces define behavior specifications, so for subclasses that implement interfaces, it is "behavior needs to be completed according to interfaces" relative to interfaces. That sounds a bit hollow, for example. For example, dog is a general term for all dog animals, peking is a dog, sheepdog is a dog, then the general characteristics of dogs will be found in peking and sheepdog, then dogs belong to the abstract type of such things relative to peking and sheepdog; and for the action of "barking," dogs can bark, birds can bark. Obviously, the former corresponds to abstract classes, while the latter refers to interfaces.

The second difference is that when an abstract class defines a type method, it may or may not give the implementation part of the method; for an interface, the methods defined therein cannot give the implementation part.

For example:

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

The third difference is that inheritance classes have different implementations of the methods involved. Inheritance classes for abstract methods defined by abstract classes, you can not override, that is, you can extend the abstract class methods; and for methods or attributes defined by interface classes, in the inheritance class must be given the corresponding method and attribute implementation.

Difference 4: In an abstract class, if you add a method, you can do nothing in the inherited class; for an interface, you need to modify the inherited class to provide a newly defined method.

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

The first advantage is that interfaces can act not only on reference types, but also on value types. Abstract classes, on the other hand, can only work on reference types.

The second advantage is that. Net type inheritance can only be single inheritance, that is, 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 become chaotic.

Third, because interfaces only define properties and methods, and have little to do with the actual type of implementation, interfaces can be reused by multiple types. 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 five, reduce the value of the type of unpacking operation. For the value type data defined by Struct, when it is stored in the collection, every time it is taken out, it is necessary to perform unpacking operation. In this case, the method of combining Struct+Interface is adopted to reduce the unpacking operation.

Interfaces have so many advantages over abstract classes, but interfaces have a fatal weakness, that is, the methods and properties defined by interfaces can only be relative to the types that inherit them (unless the function identifier defined by the interface is modified in the inheritance class), so when it comes to multi-level inheritance relations, it is difficult to implement interfaces alone. Because if you let each type inherit the interface and implement it, first of all, not to mention writing code is more cumbersome, sometimes the result of execution is still wrong, especially when the subtype object is implicitly converted to the base class object for access.

Then at this time, you need to use the interface to combine virtual methods to implement. 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 inheritance class. For abstract classes, the implementation of the method defined by the abstract class runs through the inheritance tree, so the implementation or rewriting of the method is uncertain. Therefore, abstractions are relatively more flexible than interfaces.

A simple table comparing the two is given below.

interface

abstract class

multiple inheritance

support

not support

type restrictions

no

Yes, it can only be reference type

method realizes

The method implementation must be given in the inheritance type

Inheritance classes may not be given

scalability

more troublesome

relatively flexible

multiple inheritance

More troublesome, need to rely on virtual function

more flexible

In general, interfaces and abstract classes are the linguistic means. Net provides for better implementation of inheritance relationships between types, and the two are somewhat complementary. So I don't emphasize what to use and what not to use, so the key is how to apply these two methods to the program reasonably, which is crucial.

Thank you for reading! About "what is the difference between interfaces and classes in. NET" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report