Difference Between Abstract Class and Interface in C#
π‘ Concept: Abstract Class vs Interface
Abstract classes and interfaces are used to define contracts in object-oriented programming. While both allow defining methods that derived classes must implement, they serve different purposes and follow distinct rules in C#.
π Quick Intro
An abstract class allows partial implementation and can include fields and constructors. An interface is a pure contract that specifies what a class should do, but not how. Interfaces allow multiple inheritance, whereas classes support only single inheritance.
π§ Analogy
Think of an abstract class as a partially built building with basic utilities like plumbing installed, while an interface is a checklist of items (doors, windows, rooms) that the builder must implement from scratch. Abstract classes help provide structure; interfaces help enforce rules.
π§ Technical Explanation
- Abstract Class: Can have implementation, fields, and constructors.
- Interface: Only declares members (until C# 8, where default implementations are allowed).
- Inheritance: A class can inherit one abstract class but implement multiple interfaces.
- Access Modifiers: Abstract class members can have access modifiers; interface members are public by default.
- Polymorphism: Both support polymorphism, but interfaces offer greater flexibility for loose coupling.
π― Use Cases
- β Use interfaces to define contracts across unrelated classes.
- β Use abstract classes when you need a base class with default implementations.
- β Abstract classes are useful for shared functionality; interfaces for plug-and-play behavior.
- β Use interfaces with dependency injection and unit testing frameworks.
π» Code Example
// Abstract class example
public abstract class Vehicle {
public abstract void Start();
public void Stop() {
Console.WriteLine(""Stopping..."");
}
}
// Interface example
public interface IElectric {
void Charge();
}
// Derived class implementing both
public class Tesla : Vehicle, IElectric {
public override void Start() {
Console.WriteLine(""Starting electric vehicle..."");
}
public void Charge() {
Console.WriteLine(""Charging battery..."");
}
}

β Interview Q&A
Q1: Can abstract classes have fields?
A: Yes, they can.
Q2: Can interfaces have fields?
A: No, interfaces cannot have fields.
Q3: Can interfaces define constructors?
A: No, constructors are not allowed in interfaces.
Q4: Can we create an object of an abstract class?
A: No, abstract classes cannot be instantiated.
Q5: Can an abstract class implement an interface?
A: Yes, and it may defer implementation to subclasses.
Q6: What is the default access modifier of interface members?
A: Public.
Q7: Can abstract classes include implemented methods?
A: Yes.
Q8: Can interfaces have default methods in modern C#?
A: Yes, from C# 8 onward.
Q9: Can a class extend an abstract class and implement interfaces?
A: Yes.
Q10: Can we override a non-abstract method from abstract class?
A: Only if itβs marked virtual.
π MCQs
Q1. What is a key feature of abstract classes?
- They can’t be inherited
- Only for static members
- They can have method implementations
- None
Q2. What’s true about interfaces (C# < 8)?
- Can have fields
- No implementation allowed
- Have constructors
- Access modifiers supported
Q3. Which supports multiple inheritance?
- Abstract classes
- Interfaces
- Static classes
- Delegates
Q4. Can an abstract class have constructors?
- No
- Yes
- Only private
- Only static
Q5. Can interfaces contain properties?
- No
- Yes
- Only static
- Only inherited
Q6. Which is better for DI and mocking?
- Abstract classes
- Interfaces
- Static classes
- Records
Q7. Can abstract classes define private methods?
- No
- Yes
- Only virtual
- Only protected
Q8. What’s used for common behavior and partial implementation?
- Interface
- Record
- Static class
- Abstract class
Q9. What is required to override an abstract method?
- Use partial
- Use sealed
- Must be in a derived class
- Must be private
Q10. Interfaces members default to which access?
- Private
- Protected
- Internal
- Public
π‘ Bonus Insight
You can combine both: use abstract class for shared implementation and interfaces to define additional contracts, giving the best of both inheritance and flexibility.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!