Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Abstract classes vs. interfaces in C#

Compare C# abstract classes and interfaces: state, constructors, multiple inheritance, default members, versioning and a practical rule for choosing between them.

Both an abstract class and an interface describe a contract that concrete types must fulfil, and neither can be instantiated on its own. They solve different problems. An abstract class is a partly built base type: it can hold fields, constructors and shared code, and a class can inherit only one of them. An interface is a pure capability: a set of members that any type, in any hierarchy, can promise to provide, and a type can implement many.

Since C# 8 interfaces may contain default implementations, which blurs the line, but the core difference remains: an interface has no instance state and no constructors.

C#
abstract class Repository { protected int count; public abstract void Save(); }
interface ISavable { void Save(); }

Try it

C# Shared code versus capabilities

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# What only one of them can do

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Comparison

Feature Abstract class Interface
Instantiate directly No No
Inheritance count One base class Any number
Instance fields and constructors Yes No
Static members Yes Yes (including static abstract members)
Implementation of members Any mix of abstract and concrete Optional default members (C# 8+)
Default access of members private public
Struct can use it No Yes
Adding a member later Safe if non-abstract Safe only with a default implementation

Usage notes

  • Pick an interface for a role. IDisposable, IComparable<T>, IEnumerable<T> describe what something can do. Many unrelated types can play the role, and structs can too.
  • Pick an abstract class for a family. When all implementations share state, a constructor, or a skeleton algorithm (the template method pattern), an abstract base avoids copy-paste.
  • Combine them. The common design is an interface for consumers plus an optional abstract base that implements it for convenience. Consumers depend on the interface, so they are not tied to the base.
  • Interfaces are easier to test and mock. Dependency injection typically registers an interface against a class.
  • Versioning. Adding an abstract member to a published abstract class breaks every derived class, and adding a plain member to an interface breaks every implementer unless you supply a default implementation.
  • Boxing. Calling an interface method on a struct through the interface boxes it. Constrain generics with where T : IFoo to avoid that.
  • Do not create an interface for every class. One implementation with no second consumer in sight is usually just noise.