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.