Abstract Class vs Interface in C#: Key Differences and Usage
abstract class when you want to share common code among related classes and provide default behavior. Use an interface when you want to define a contract that multiple unrelated classes can implement without sharing code.Quick Comparison
This table summarizes the main differences between abstract classes and interfaces in C#.
| Factor | Abstract Class | Interface |
|---|---|---|
| Purpose | Share common code and base functionality | Define a contract without implementation |
| Multiple inheritance | No, a class can inherit only one abstract class | Yes, a class can implement multiple interfaces |
| Members allowed | Methods with or without implementation, fields, properties | Only method/property/event signatures (default implementations allowed since C# 8) |
| Access modifiers | Supports access modifiers (public, protected, etc.) | Members are public by default, no access modifiers allowed |
| When to use | When classes are closely related and share code | When unrelated classes need to follow the same contract |
| Constructors | Can have constructors | Cannot have constructors |
Key Differences
An abstract class allows you to define some common behavior and state that derived classes can reuse or override. It can contain fields, constructors, and fully implemented methods. This makes it ideal when you have a clear class hierarchy where subclasses share code.
On the other hand, an interface defines a set of methods and properties that implementing classes must provide. It does not hold state or implementation (except default interface methods introduced in C# 8). Interfaces enable multiple inheritance of type, allowing a class to implement many interfaces, which is useful for defining capabilities across unrelated classes.
Another key difference is that abstract classes can control access to members with modifiers like protected, while interface members are always public. Also, abstract classes can have constructors to initialize state, but interfaces cannot.
Code Comparison
using System; abstract class Animal { public abstract void Speak(); public void Eat() { Console.WriteLine("Eating food..."); } } class Dog : Animal { public override void Speak() { Console.WriteLine("Woof!"); } } class Program { static void Main() { Dog dog = new Dog(); dog.Speak(); dog.Eat(); } }
Interface Equivalent
using System;
interface IAnimal
{
void Speak();
void Eat();
}
class Dog : IAnimal
{
public void Speak()
{
Console.WriteLine("Woof!");
}
public void Eat()
{
Console.WriteLine("Eating food...");
}
}
class Program
{
static void Main()
{
Dog dog = new Dog();
dog.Speak();
dog.Eat();
}
}When to Use Which
Choose an abstract class when:
- You have a clear class hierarchy with shared code or state.
- You want to provide default method implementations and control access modifiers.
- You need constructors to initialize common data.
Choose an interface when:
- You want to define a contract for unrelated classes.
- You need multiple inheritance of types.
- You want to ensure classes implement certain methods without forcing shared code.