What is the difference between an abstract class and an interface in C#?
Learn the difference between abstract classes and interfaces in C#: single vs multiple inheritance, shared state, contracts, and when to use each with examples.
Expected Interview Answer
An abstract class is a partially implemented base class that can hold state, constructors, and shared logic, while an interface is a pure contract that declares members a type must implement, letting a class satisfy many interfaces but inherit only one class.
An abstract class can contain fields, constructors, access modifiers, and both abstract and concrete members, and a class can inherit only one of them. An interface traditionally declared only signatures, though modern C# (8.0+) allows default implementations, static members, and more. Use an abstract class to share code and state across closely related types; use an interface to describe a capability that unrelated types can all promise to provide.
- Abstract classes share common implementation and state
- Interfaces enable multiple inheritance of type
- Interfaces decouple callers from concrete types
- Abstract classes can enforce construction via constructors
- Both support polymorphism and testable designs
AI Mentor Explanation
An abstract class is like a partly-filled team coaching manual: it already prescribes the warm-up drills and fitness routine every player must do, but leaves the batting or bowling technique blank for each specialist to complete. An interface is like the laws of cricket every player signs up to obey regardless of role — it dictates what must happen but supplies no ready-made routine or shared kit.
Step-by-Step Explanation
Step 1
Decide: share code or declare capability
If closely related types need shared state and logic, reach for an abstract class; if unrelated types just need to promise behaviour, use an interface.
Step 2
Define the abstract class
Mark the class abstract, add fields, a constructor, concrete helpers, and abstract members that derived classes must override.
Step 3
Define the interface
Declare method, property, and event signatures the implementer must provide; optionally add default implementations in C# 8+.
Step 4
Implement in a concrete type
A class inherits one abstract base with a colon and implements any number of interfaces in the same base list.
Step 5
Consume via the abstraction
Program against the abstract type or interface so callers stay decoupled from concrete implementations.
What Interviewer Expects
- Single vs multiple inheritance distinction
- That abstract classes can hold state and constructors
- That interfaces are contracts, not implementations
- Awareness of C# 8+ default interface members
- A clear rule of thumb for choosing between them
Common Mistakes
- Claiming a class can inherit multiple abstract classes
- Saying interfaces can never have any implementation (untrue since C# 8)
- Forgetting interfaces cannot hold instance fields
- Using an abstract class where an interface would decouple better
- Not knowing abstract classes can have constructors
Best Answer (HR Friendly)
“An abstract class is a half-finished blueprint you extend to reuse shared code, and a class can only build on one of them. An interface is a checklist of abilities a class promises to provide, and a class can promise to fulfil many interfaces at once.”
Code Example
public interface ILogger
{
void Log(string message);
}
public abstract class ShapeBase
{
protected ShapeBase(string name) => Name = name;
public string Name { get; }
public abstract double Area(); // must override
public void Describe() => // shared logic
Console.WriteLine($"{Name} area = {Area()}");
}
public class Circle : ShapeBase, ILogger
{
private readonly double _r;
public Circle(double r) : base("Circle") => _r = r;
public override double Area() => Math.PI * _r * _r;
public void Log(string message) => Console.WriteLine(message);
}Follow-up Questions
- Can an abstract class implement an interface?
- What changed with default interface methods in C# 8?
- When would you prefer composition over an abstract base class?
- Can you instantiate an abstract class directly?
- How do explicit interface implementations resolve name clashes?
MCQ Practice
1. How many abstract classes can a C# class inherit from directly?
C# supports single class inheritance, so a class may derive from only one abstract (or concrete) base class, though it can implement many interfaces.
2. Which can hold instance fields and a constructor?
Abstract classes can declare instance fields and constructors; interfaces cannot hold instance state or constructors.
3. Which C# version first allowed default method implementations in interfaces?
C# 8.0 introduced default interface members, letting interfaces ship a default body without breaking existing implementers.
Flash Cards
Key inheritance limit of abstract classes? — A class can inherit only one abstract (or concrete) base class.
Key inheritance ability of interfaces? — A class can implement any number of interfaces.
Can an abstract class have a constructor? — Yes, to initialize shared state for derived classes.
Can an interface hold instance fields? — No, interfaces cannot declare instance fields.
Rule of thumb? — Abstract class to share code/state; interface to declare a capability across unrelated types.