Abstract Class vs Interface in C#: Key Differences and Usage
abstract class can provide both method implementations and abstract methods, allowing shared code and state, while an interface only declares method signatures without implementations. Use abstract classes when you want to share common behavior and state, and interfaces to define a contract that multiple unrelated classes can implement.Quick Comparison
This table summarizes the main differences between abstract classes and interfaces in C#.
| Feature | Abstract Class | Interface |
|---|---|---|
| Purpose | Base class with shared code and abstract methods | Defines a contract with method signatures only |
| Method Implementation | Can have both implemented and abstract methods | Cannot have method implementations (before C# 8.0) |
| Fields/State | Can have fields and constructors | Cannot have fields or constructors |
| Multiple Inheritance | Cannot inherit from multiple abstract classes | Can implement multiple interfaces |
| Access Modifiers | Can have access modifiers (public, protected, etc.) | All members are public by default |
| Version Introduced | Since early C# versions | Since early C# versions; default implementations added in C# 8.0 |
Key Differences
Abstract classes allow you to define some common behavior and state that derived classes can share. They can have fields, constructors, and fully implemented methods alongside abstract methods that must be overridden. This makes them suitable when classes are closely related and share a base implementation.
On the other hand, interfaces define a contract that classes must follow, specifying what methods or properties they must implement without providing any code (except default implementations starting from C# 8.0). Interfaces support multiple inheritance, allowing a class to implement many interfaces, which is useful for defining capabilities across unrelated classes.
In summary, use abstract classes when you want to share code and state, and use interfaces to enforce a contract across different class hierarchies without sharing implementation.
Code Comparison
Here is an example of an abstract class defining a shape with a shared method and an abstract method to calculate area.
using System; abstract class Shape { public string Name { get; set; } public Shape(string name) { Name = name; } public void Display() { Console.WriteLine($"Shape: {Name}"); } public abstract double GetArea(); } class Circle : Shape { public double Radius { get; set; } public Circle(double radius) : base("Circle") { Radius = radius; } public override double GetArea() { return Math.PI * Radius * Radius; } } class Program { static void Main() { Circle c = new Circle(3); c.Display(); Console.WriteLine($"Area: {c.GetArea():F2}"); } }
Interface Equivalent
The same shape example using an interface to define the contract without shared implementation or state.
using System;
interface IShape
{
string Name { get; }
double GetArea();
}
class Circle : IShape
{
public string Name => "Circle";
public double Radius { get; set; }
public Circle(double radius)
{
Radius = radius;
}
public double GetArea()
{
return Math.PI * Radius * Radius;
}
}
class Program
{
static void Main()
{
IShape c = new Circle(3);
Console.WriteLine($"Shape: {c.Name}");
Console.WriteLine($"Area: {c.GetArea():F2}");
}
}When to Use Which
Choose an abstract class when: you want to share common code or state among closely related classes, need constructors or fields, and want to provide default behavior that derived classes can override.
Choose an interface when: you want to define a contract that multiple unrelated classes can implement, need to support multiple inheritance of types, or want to specify capabilities without enforcing a class hierarchy.