In C#, Abstract Class and Interface are both used for abstraction, but they serve different purposes.
Quick Difference (Interview Answer)
-
Abstract Class → Used when classes share common behavior + base implementation
-
Interface → Used when classes share only contract (no implementation)
1. Abstract Class
An Abstract Class can have:
✅ Abstract methods
✅ Concrete methods
✅ Fields
✅ Constructors
✅ Properties
Example
public abstract class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating");
}
public abstract void MakeSound();
}
Child class:
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog barks");
}
}
Usage
Dog dog = new Dog();
dog.Eat();
dog.MakeSound();
2. Interface
An Interface only defines contract (what to implement)
Example
public interface IAnimal
{
void Eat();
void MakeSound();
}
Child class:
public class Dog : IAnimal
{
public void Eat()
{
Console.WriteLine("Dog is eating");
}
public void MakeSound()
{
Console.WriteLine("Dog barks");
}
}
Key Differences Table
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods with body | ✅ Yes | ⚠️ Yes (C# 8+) |
| Abstract methods | ✅ Yes | ✅ Yes |
| Fields | ✅ Yes | ❌ No |
| Constructors | ✅ Yes | ❌ No |
| Access Modifiers | ✅ Yes | ❌ No (Mostly public) |
| Multiple inheritance | ❌ No | ✅ Yes |
| When to use | Base class | Contract |
Important Interview Question
Multiple Inheritance
Abstract class ❌ Not allowed
class A {}
class B {}
class C : A, B // ❌ Not allowed
{
}
Interface ✅ Allowed
interface IA {}
interface IB {}
class C : IA, IB // ✅ Allowed
{
}
When to Use Abstract Class
Use Abstract Class when:
✅ Classes are closely related
✅ Want common functionality
✅ Need shared code
Example:
-
Animal → Dog, Cat
-
Employee → Manager, Developer
When to Use Interface
Use Interface when:
✅ Different classes share behavior
✅ Need multiple inheritance
✅ Want loose coupling
Example:
-
ILogger
-
IRepository
-
IEmailService
Real-World Example
interface ILogger
{
void Log(string message);
}
class FileLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine("File log");
}
}
class DbLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine("DB log");
}
}
Interview One-Line Answer
Abstract class provides partial implementation, while Interface provides only contract. Abstract class supports single inheritance, Interface supports multiple inheritance.
Trick Interview Question
Q: Can interface have implementation?
👉 Yes, after C# 8.0 (Default Interface Methods)
public interface ITest
{
void Test()
{
Console.WriteLine("Default implementation");
}
}
