🧩 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();
}

🏗️ 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...");
    }
}

🔍 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:

✅ Use Abstract Class When:

📌 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:

Understanding their differences is key to writing clean, flexible, and maintainable C# code.