Should I use an abstract class or an interface?
The functionality available through the use of abstract classes and of interfaces is quite similar. However, each has its pros and cons.
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.
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.
An abstract class can contain an interface plus implementations. This simplifies versioning. An abstract class can be extended by adding new nonabstract 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.
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.
Use 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.
Use 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.
The following table compares the features of abstract classes and interfaces.
|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 (|| All members are |
|Can include data fields.||Cannot include data fields. However, abstract properties may be declared.|
- .NET Type Design Guidelines
- Choosing between Abstract and Interface
- Classes, Structs, and Objects
- Implementing Interfaces
- Interface-Based Programming
- Test for an interface implementation
- Using Inheritance Effectively