The difference between an abstract class and an interface

Source: Internet
Author: User
Tags comparison table

. NET provides interfaces, which are different from class or struct type definitions. Interface There are situations that look like abstract classes, so some people think that in. NET can replace abstract classes entirely with interfaces. In fact, interfaces and abstract classes each have strengths and weaknesses, so often in the application, the two should be combined to use, thereby complementing the length.

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

Difference one, the concept of the two expressions is different. Abstract class is a kind of high aggregation of things, then for the subclass of the inheritance abstract class, for the abstract class, is a "yes" relationship, and the interface is to define the behavior specification, so to implement the interface subclass, relative to the interface, is "behavior needs to follow the interface to complete." This may sound a little hollow, for example. For example, a dog is a general term for all dog animals, Beijing-Kazakhstan is a dog, the dog is a dog, then the common characteristics of dogs, will be found in Beijing-Kazakhstan, the shepherd, then the dog is relative to the Beijing-Kazakhstan and the shepherd, it belongs to the abstract type of such things, and for "called" This action, the dog can It is clear that the former is equivalent to an abstract class, while the latter refers to an interface.

The difference between two, the abstract class in the definition of the type method, you can give the implementation of the method of the part, or can not be given, and for the interface, the method defined in it cannot give the implementation part.

For example:

Public abstract class Abstest
public virtual void Test ()
Debug.WriteLine ("Test");
public abstract void Newtest ();
public interface ITest
void Test ();
void Newtest ();

The difference is three, the inheriting class is different to the implementation of the method involved. The abstract method that the inheriting class defines for the abstract class can be used without rewriting, that is to say, the method of the abstract class can be used, and for the method or property defined by the interface class, the corresponding method and property must be implemented in the inheriting class.

The difference four, in the abstract class, the addition of a method, the inheritance class can not do any processing, and for the interface, you need to modify the inheritance class, provide a new definition of the method.

Know the difference between the two, and then say that the interface relative to the advantages of the abstract class.

Benefit one, interfaces can be used not only for reference types, but also for value types. Abstract classes, for example, can only be used for reference types.

Benefit two,. NET type inheritance can only be single-inheritance, that is, a type must inherit only one type, but may inherit multiple interfaces. In fact, I agree with this point, multi-inheritance will make the inheritance tree become chaotic.

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

Benefit four, through the interface, you can reduce the types of exposed properties and methods, making it easier to protect type objects. When a type that implements an interface may contain other methods or properties, but when the method returns, the interface object can be returned, so that the caller is able to access related elements of the object only through the methods or properties provided by the interface, which effectively protects the other elements of the object.

Benefit five, reducing the value type of the unboxing operation. For the value type data defined by the struct, when the collection is taken out, the unpacking operation is required, and the Struct+interface method is used to reduce the unpacking operation.

The interface has so many advantages over abstract classes, but the interface has a fatal weakness, that is, the interface defines methods and properties that can only be compared to the type that inherits it (unless the function label of the interface definition is modified in the inheriting class), then the optical interface is difficult to implement for the multi-layer inheritance relationship. Because if each type is to inherit the interface and implement it, first of all do not say that writing code is cumbersome, sometimes the result of execution is still wrong, especially when the subtype object is implicitly converted to the base class object for access.

At this time, it is necessary to use the interface with virtual method to achieve. In fact, in the inheritance, whether to use an interface or abstract class. Interfaces are fixed and conventional, so the implementation of the corresponding methods and properties of the interface must be provided in the inheriting class. For abstract classes, the implementation of the definition method of abstract classes runs through the entire inheritance tree, so that the implementation or rewrite of the method is undefined. Therefore, the abstract analogy interface is more flexible.

A simple comparison table for both is given below.


Abstract class

Multiple inheritance


Not supported

Type restrictions


Yes, only reference type

Method implementation

The method implementation must be given in the inheritance type

The inheritance class can not give a


More Trouble

Relatively flexible

Multilayer inheritance

More trouble, need to use virtual function

More flexible


  In general, interfaces and abstract classes are. NET in order to better implement the type of inheritance between types of language means, and the two have a complementary relationship. Therefore, I do not emphasize what to use and what is not, then the crux of the problem is how to apply these two means reasonably to the program, which is crucial.

The difference between an abstract class and an interface

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: 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.