๐งฉ Interface vs Abstract Class in C#
In C#, both interfaces and abstract classes are used to achieve abstraction and polymorphism, two of the core pillars of Object-Oriented Programming (OOP). However, they serve different purposes and have distinct characteristics. Understanding their differences helps you design better, more maintainable, and scalable applications.
๐ง What is an Interface?
An interface defines a contract that implementing classes must follow. It only contains the signatures of methods, properties, events, or indexers — without any implementation (except for default interface methods introduced in C# 8.0+).
public interface IAnimal
{
void Eat();
void Sleep();
}
- No fields or constructors
- No access modifiers (everything is public by default)
- Supports multiple inheritance
๐๏ธ What is an Abstract Class?
An abstract class is a class that cannot be instantiated on its own and may contain both abstract members (without implementation) and concrete members (with implementation).
public abstract class Animal
{
public abstract void Eat();
public void Breathe()
{
Console.WriteLine("Breathing...");
}
}
- Can have fields, constructors, and implemented methods
- Can define access modifiers (public, protected, etc.)
- Supports single inheritance
๐ Key Differences at a Glance
Feature |
Interface |
Abstract Class |
Implementation |
No implementation (except default methods) |
Can have both abstract and concrete methods |
Multiple inheritance |
โ
Yes (a class can implement multiple interfaces) |
โ No (only one abstract class can be inherited) |
Fields |
โ Not allowed |
โ
Allowed |
Constructors |
โ Not allowed |
โ
Allowed |
Access Modifiers |
โ Not supported (everything is public) |
โ
Fully supported |
Performance |
Slightly better due to method dispatching |
May add overhead due to inheritance hierarchy |
Default Implementation (C# 8+) |
โ
Yes |
โ
Yes |
Use Case |
Best for defining capability contracts |
Best for defining base class behavior |
๐งช Code Example: Interface vs Abstract Class
๐ธ Using Interface
public interface IWorker
{
void Work();
void Report();
}
public class Developer : IWorker
{
public void Work() => Console.WriteLine("Coding...");
public void Report() => Console.WriteLine("Reporting progress...");
}
๐น Using Abstract Class
public abstract class Worker
{
public abstract void Work();
public void Report()
{
Console.WriteLine("Reporting to manager...");
}
}
public class Tester : Worker
{
public override void Work()
{
Console.WriteLine("Testing software...");
}
}
๐งฐ When to Use What?
โ
Use Interface When:
- You need to define a contract with no default behavior.
- You need to support multiple inheritance.
- You're creating plugin-style or loosely-coupled architectures.
โ
Use Abstract Class When:
- You need to share common behavior among related classes.
- You want to define default behavior that can be overridden.
- You need to include fields or constructors.
๐ C# 8+ and Default Interface Methods
With C# 8 and later, interfaces can now contain default method implementations:
public interface ILogger
{
void Log(string message);
void LogInfo(string message)
{
Console.WriteLine("INFO: " + message);
}
}
However, this should be used cautiously, as it can blur the line between interfaces and abstract classes.
๐ Conclusion
While interfaces and abstract classes both support abstraction, they serve different purposes:
- Use interfaces for defining contracts or capabilities.
- Use abstract classes when you need shared implementation logic.
Understanding their differences is key to writing clean, flexible, and maintainable C# code.