Click here to Skip to main content
Click here to Skip to main content

Comparison of Interface and Abstract Class and Use

By , 20 Dec 2011
 

Introduction

The functionality available through the use of abstract classes and of interfaces is quite similar. However, each has its pros and cons.

Inheritance

A C# class may only subclass—inherit from—one other class. Therefore, by inheriting from (subclassing) an abstract class, the derived class has used up its ability to participate in a meaningful type hierarchy.

On the other hand, a class can implement—inherit from—any number of interfaces. And, it can still inherit from (subclass) a base class which makes sense.

Single Inheritance

C# supports single inheritance: C# does not support multiple inheritance from multiple classes like C++ does. But, by using abstract classes and interfaces, C# programs can achieve most of the same functionality without the confusion and maintenance problems associated with multiple inheritance.

Value Type Polymorphism

.NET value types are objects descending from Object; but, they cannot inherit from other types. They can implement interfaces. Thus, primitives—such as Int32— can implement the IComparable interface, for example, making them comparable.

Separation of Contract and Implementation

Interfaces separate the syntax rather than the semantic contract from the implementation. Classes can be designed to decouple the semantic contract from the implementation. For example, abstract classes can be separated in a different assembly than their concrete implementations.

Versioning

An abstract class can contain an interface plus implementations. This simplifies versioning. An abstract class can be extended by adding new non abstract methods with default implementations. Also, a convenience method is easily added to an abstract class.

An interface cannot be modified without breaking its contract with the classes which implement it. Once an interface has been shipped, its member set is permanently fixed. An API based on interfaces can only be extended by adding new interfaces.

Design Flexibility

Interfaces offer more design flexibility; precisely because, they can be implemented by any class regardless of its type hierarchy.

Visual C# Best Practices

Use abstract classes and interfaces in combination to optimize your design trade-offs.

Using an Abstract Class

When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)

Use an abstract class to define a common base class for a family of types.

Use an abstract class to provide default behavior.

Subclass only a base class in a hierarchy to which the class logically belongs.

Using an Interface

When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because it offers more design flexibility.

Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.

Use an interface to design a polymorphic hierarchy for value types.

Use an interface when an immutable contract is really intended.

A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

Summary

The following table compares the features of abstract classes and interfaces:

Abstract class Interface
Derived classes exhaust their single base class inheritance option. Classes can implement multiple interfaces without using up their base class option. But, there are no default implementations.
Cannot be instantiated except as part of subclasses. Only derived classes can call an abstract class constructor. Cannot be instantiated.
Defines abstract member signatures which derived classes must implement. Otherwise, the derived class itself will be abstract. Defines abstract member signatures—all of which—implementing classes must implement. Otherwise, a compiler error results.
New non-abstract members may be added that derived classes will inherit without breaking version compatibility. Extending an interface with new members breaks version compatibility.
Optionally, provide default (virtual) member implementation. All members are virtual and cannot provide implementations.
Can include data fields. Cannot include data fields. However, abstract properties may be declared.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

md_refay
Web Developer
Egypt Egypt
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Suggestionmore info about Abstract in .netmemberAryan_MSD14 Jun '12 - 19:39 
visit my blog for some more info on Abstract objects in .net
http://viralpanchal.wordpress.com/2012/06/14/abstract-classes-in-net/[^]
Questionnice articlemembersatishmachineni4 Apr '12 - 1:27 
well explained
GeneralMy vote of 5memberDuffmanLight2 Jan '12 - 9:26 
Nice summary of abstract classes vs. interfaces.
GeneralMy vote of 5memberPranit Kothari27 Dec '11 - 16:09 
Thx

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 20 Dec 2011
Article Copyright 2011 by md_refay
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid