Differences between abstract classes and interfaces [a good article is to add to favorites!

Source: Internet
Author: User

. NetProvides interfaces, which are different fromClassOrStruct. In some cases, the interface seems to be the same as the abstract class, so some people think that. NetThe abstract class can be completely replaced by interfaces. In fact, interfaces and abstract classes have their own strengths and weaknesses. Therefore, they are often used together in applications to complement each other.

 

Next, let's talk about the differences between abstract classes and interfaces.

Difference 1: the two concepts are different. Abstract classes are highly aggregated for a class of things. For subclass inheriting abstract classes, abstract classes belong to the "yes" relationship, while interfaces define behavior specifications, therefore, for the subclass that implements the interface, compared with the interface, it is "the behavior needs to be completed according to the interface ".These sounds a bit false. For example. For example, a dog is a general term for all dogs and animals. If a dog is a dog and a dog is a dog, the general characteristics of the dog will be found in jingha and a sheepdog, compared with jingha and sheepdog, a dog belongs to the abstract type of such things. For the action of "calling", a dog or a bird can also be called. Obviously, the former is equivalent to an abstract class, while the latter is an interface.

 

Difference 2: when defining a type method in an abstract class, the implementation part of the method can be provided or not. For an interface, the implementation part of the method defined in the abstract class cannot be provided.

For example:

Public Abstract ClassAbstest

{

Public Virtual VoidTest ()

{

Debug. writeline ("test ");

}

Public Abstract VoidNewtest ();

}

Public InterfaceItest

{

VoidTest ();

VoidNewtest ();

}

 

Difference 3: The Inheritance class has different implementations for the methods involved in the two. For abstract methods defined by abstract classes, the inherited classes do not need to be overwritten. That is to say, they can be extended. For methods or attributes defined by interface classes, corresponding methods and attribute implementations must be provided in the inheritance class.

 

Difference 4: In an abstract class, if you add a method, the inheritance class does not need to be processed. For an interface, you need to modify the inheritance class and provide a new defined method.

 

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

Benefit 1: An interface can be applied not only to the reference type, but also to the value type. Abstract classes can only act on the reference type.

 

Benefit 2,. NetOnly one type can be inherited, that is, one type can only inherit one type, but can inherit multiple interfaces.In fact, I agree with this point. Multi-inheritance will make the inheritance tree messy.

 

Benefit 3: Because the interface only defines attributes and methods, but has little to do with the actually implemented types, the interface can be reused by multiple types. Abstract classes are more closely related to inherited classes.

 

Benefit 4: through interfaces, you can reduce the type exposure attributes and methods, so as to facilitate the protection of type objects.When an interface type may contain other methods or attributes, but a method returns an interface object, the caller can only use the methods or Attributes provided by the interface, access the related elements of the object, which can effectively protect other elements of the object.

 

Benefit 5: Reduce the value-type unpacking operation.ForStructData of the defined value type. When a collection is stored and retrieved, The binning operation is required.Struct + InterfaceTo reduce the binning operation.

SeeArticleProvided.

Http://blog.csdn.net/Knight94/archive/2006/10/08/1326326.aspx

 

Compared with abstract classes, interfaces have many advantages, but interfaces have a fatal weakness, that is, the methods and attributes defined by an interface can only be relative to the type inherited from it (unless you modify the function ID defined by the interface in the inheritance class). For multi-layer inheritance relationships, it is difficult to implement an interface only.. Because if each type is implemented by inheriting the interface, do not writeCodeThe execution result is sometimes incorrect, especially when the child type objects are implicitly converted to base class objects for access.

At this time, you need to use interfaces and virtual methods. SeeIdisposableThe implementation method in the inheritance type.

Http://blog.csdn.net/Knight94/archive/2006/10/10/1329214.aspx

 

In fact, in inheritance, the interface or abstract class is used. The interface is fixed and conventional. Therefore, the corresponding methods and attributes of the interface must be provided in the inheritance class. For an abstract class, the implementation of the definition method of the abstract class runs through the entire inheritance tree. Therefore, the implementation or rewriting of the method is uncertain. Therefore, abstract analogy interfaces are more flexible.

 

The following is a simple comparison table between the two.

 

Interface

Abstract class

Multi-Inheritance

Supported

Not Supported

Type restrictions

No

Yes. It can only be of the reference type.

Method implementation

Method implementation must be provided in the inheritance type

The inheritance class can be left empty.

Scalability

Relatively troublesome

Relatively flexible

Multi-layer inheritance

It is troublesome. You need to use virtual functions.

Flexible

 

In general, interfaces and abstract classes are. NetLanguage means provided to better implement the inheritance relationship between types, and the two are complementary. Therefore, the key to the problem is how to apply the two methods reasonably.ProgramThis is crucial.

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1357947

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.