Differences between interfaces and abstract classes in C #

Source: Internet
Author: User
This article from: http://horseych.blog.sohu.com/84223160.html
. NET provides interfaces, which are different from class or struct type definitions. In some cases, the interface seems to be the same as the abstract class, so some people think that the. NET interface can be used to replace the abstract class completely. 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 type of things. Therefore, for subclasses that inherit abstract classes, they belong to the "yes" relationship. 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 class abstest
{
Public Virtual void test ()
{
Debug. writeline ("test ");
}
Public abstract void newtest ();
}
 
Public interface itest
{
Void test ();
Void newtest ();
}
 
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:. Net type inheritance can only be a single inheritance, that is, a 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. For the value-type data defined by struct, whenever a collection is stored, it is necessary to unpack the data. In this case, the combination of struct and interface is used to reduce the unpacking operation.
See the methods provided in the following article.

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 method and attribute defined by the interface can only be relative to the type inherited from it (unless the Function Identifier defined by the excuse is modified in the inheritance class). For multi-layer inheritance relationships, it is difficult to implement an interface. If each type is implemented by inheriting the interface, it is cumbersome to write the code first, and sometimes the execution result is 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. See the implementation method of idisposable 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.

Related Article

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.