Lesson 16 — Abstract Class vs Interface in C# (Key Differences & Examples)

Both abstract classes and interfaces are used to achieve abstraction in C#.
However, many beginners get confused about when to use which one.

In this lesson, you’ll learn:

  • What is an abstract class?
  • What is an interface?
  • Key differences (easy table)
  • When to use abstract class vs interface
  • Practical C# examples

Let’s make it simple and clear.


🌟 What is an Abstract Class?

An abstract class is a partially defined class that may contain both implemented and unimplemented methods.

✔ Key points:

  • Cannot be instantiated
  • Can contain abstract methods (no body)
  • Can contain normal methods (with body)
  • Can have fields, constructors, and properties
  • Child classes must implement abstract methods

🧑‍💻 Example:

public abstract class Animal
{
    public abstract void MakeSound();   // abstract method

    public void Sleep()                 // normal method
    {
        Console.WriteLine("Sleeping...");
    }
}

Child class MUST implement MakeSound().


🌟 What is an Interface?

An interface defines a contract with method signatures only.
Classes or structs that implement an interface MUST provide the method implementation.

✔ Key points:

  • 100% abstraction (before C# 8)
  • Cannot have fields
  • Cannot have constructors
  • A class can implement multiple interfaces
  • Provides flexibility and loose coupling

🧑‍💻 Example:

public interface IAnimal
{
    void MakeSound();   // no body
}


🧱 Abstract Class Example (Full Implementation)

public abstract class Vehicle
{
    public abstract void Start();  // must be overridden

    public void Stop()
    {
        Console.WriteLine("Vehicle stopped");
    }
}

public class Car : Vehicle
{
    public override void Start()
    {
        Console.WriteLine("Car starting...");
    }
}

Usage:

Vehicle v = new Car();
v.Start();
v.Stop();


🧱 Interface Example (Full Implementation)

public interface IDrive
{
    void Start();
}

public class Bike : IDrive
{
    public void Start()
    {
        Console.WriteLine("Bike starting...");
    }
}

Usage:

IDrive d = new Bike();
d.Start();


🔍 Abstract Class vs Interface — Easy Comparison Table

FeatureAbstract ClassInterface
MethodsAbstract + normalOnly abstract (before C# 8), default methods allowed in C# 8+
FieldsAllowedNot allowed
ConstructorsAllowedNot allowed
Access ModifiersSupports allAll members are public by default
Multiple InheritanceNot allowedAllowed (class can implement multiple interfaces)
Use CaseWhen objects are closely relatedWhen unrelated classes need common behavior
InheritanceOnly one parent class allowedMultiple interfaces allowed
Default ImplementationYesNo (until C# 8)

🎯 When Should You Use an Abstract Class?

Use abstract class when:

✔ You want shared code + abstract methods
✔ Your classes share a strong relationship
✔ You want to define a base class with common logic
✔ You need constructors
✔ You want fields or protected members

Example:

All vehicles have wheels, fuel, engine → use abstract class.


🎯 When Should You Use an Interface?

Use interface when:

✔ You want completely unrelated classes to follow the same contract
✔ You need multiple inheritance
✔ You want to achieve loose coupling
✔ You want to define only behavior (no state)

Example:

  • Save() method for saving data
  • Start() method for any device
  • Log() method in different classes

🌈 Real-World Analogy

✔ Abstract Class → “Family Blueprint”

A shared structure among related members.
Example: Human → Man, Woman, Child.

✔ Interface → “Job Role Contract”

Any person can become a teacher, driver, or player by following the role requirements.


🧠 Practical Example — Payment System

Interface:

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

Abstract class:

public abstract class PaymentBase
{
    public void Validate()
    {
        Console.WriteLine("Validating payment...");
    }

    public abstract void Pay(double amount);
}

Derived class:

public class UpiPayment : PaymentBase, IPayment
{
    public override void Pay(double amount)
    {
        Console.WriteLine($"Paid {amount} via UPI");
    }
}


📝 Mini Exercise

Create:

1️⃣ An abstract class Shape with method Draw() and normal method Info()
2️⃣ An interface IColor with method SetColor()
3️⃣ A class Circle that inherits Shape and implements IColor

Print output.


🔍 FAQs

Q1: Can a class implement multiple interfaces?

Yes.

class Car : IEngine, IBrake { }

Q2: Can a class inherit from multiple abstract classes?

No. C# does not support multiple inheritance.

Q3: Which is faster — interface or abstract class?

Abstract classes are slightly faster, but difference is small.

Q4: Can interfaces contain private members?

No. All interface members are public.

Q5: Can abstract classes contain fields?

Yes.


🎉 Conclusion

You now understand:

✔ What abstract classes are
✔ What interfaces are
✔ Key differences
✔ When to use which
✔ Detailed C# examples
✔ Best practices and analogies

Abstract classes create strong hierarchies, while interfaces provide flexibility and multiple inheritance.


Next lesson: