Abstract Class vs Interface in C# – When to Use What (With Examples)

Introduction

Abstract vs Interface in C# is one of the most commonly asked questions in .NET interviews.
Many developers understand the definitions but struggle to explain when to use abstract class and when to use interface in real-world applications.

“What is the difference between Abstract Class and Interface?”

Many developers know the definitions but struggle to explain:

  • When to use abstract class
  • When to use interface
  • Why one is better than the other in real projects

In this article, I’ll explain the difference in simple language, with real examples and an interview perspective.


What is an Abstract Class?

An abstract class is a class that:

  • Can have abstract methods (without implementation)
  • Can also have normal methods (with implementation)
  • Can contain fields, constructors, and access modifiers

It represents an “is-a” relationship.

Example

public abstract class Employee
{
    public int Id { get; set; }

    public abstract decimal CalculateSalary();

    public void DisplayRole()
    {
        Console.WriteLine("Employee");
    }
}

Key Points

  • Cannot be instantiated
  • Supports method implementation
  • Supports access modifiers (protected, private)
  • Supports constructors

What is an Interface?

An interface defines a contract.

It tells what should be done, not how it should be done.

Example

public interface IPaymentService
{
    void ProcessPayment(decimal amount);
}

Implementation

public class CreditCardPayment : IPaymentService
{
    public void ProcessPayment(decimal amount)
    {
        Console.WriteLine($"Processing payment of {amount}");
    }
}

Key Points

  • No implementation (before C# 8)
  • No fields
  • No constructors
  • A class can implement multiple interfaces

Key Differences (Interview-Friendly Table)

FeatureAbstract ClassInterface
Multiple inheritance❌ No✅ Yes
Method implementation✅ Yes❌ No (default only after C# 8)
Fields✅ Yes❌ No
Constructors✅ Yes❌ No
Access modifiers✅ Yes❌ No
PurposeBase classContract

Real-World Use Case Example

Scenario:

You are building a payment system.

Interface (Contract)

public interface IPayment
{
    void Pay(decimal amount);
}

Abstract Class (Shared Logic)

public abstract class PaymentBase : IPayment
{
    public void LogPayment()
    {
        Console.WriteLine("Payment logged");
    }

    public abstract void Pay(decimal amount);
}

Concrete Class

public class UpiPayment : PaymentBase
{
    public override void Pay(decimal amount)
    {
        Console.WriteLine($"UPI payment of {amount}");
    }
}

✔ Interface defines what
✔ Abstract class provides common behavior


When to Use Abstract Class?

Use abstract class when:

  • You want to share common code
  • You want to control base behavior
  • Classes are closely related
  • You need constructors or fields

When to Use Interface?

Use interface when:

  • You want multiple inheritance
  • You want loose coupling
  • You are designing frameworks
  • You want to define a contract

Common Trap

Question:

Can a class inherit from multiple abstract classes?

-> No

Question:

Can a class implement multiple interfaces?

-> Yes


One-Line Answer

Use interface to define contracts and abstract class to share common base functionality.


Conclusion

Both abstract classes and interfaces are powerful tools in C#.

The choice depends on:

  • Design requirement
  • Reusability
  • Future scalability

You may also read IQueryable vs IEnumerable in C# to understand another important concept.

1 thought on “Abstract Class vs Interface in C# – When to Use What (With Examples)”

  1. Pingback: SOLID Principles in C# – Complete Guide for Clean Code

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top