C#  

Difference between interface and abstract class?

🧩 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.