Let’s go through interfaces and abstract classes in C# — two important tools for defining contracts and shared behavior in object-oriented programming.
📌 1. Abstract Classes
An abstract class is a class that cannot be instantiated (Instantiation) directly.
-
Can have fields, properties, methods (both implemented and abstract methods)
-
Abstract methods must be implemented in derived classes
-
Used to define a common base with shared functionality
Syntax
public abstract class Shape
{
public abstract void Draw(); // Must be implemented by derived class
public void Display() // Concrete method
{
Console.WriteLine("Displaying shape...");
}
}
Example
public class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a Circle");
}
}
// Usage
Shape shape = new Circle();
shape.Draw(); // Drawing a Circle
shape.Display(); // Displaying shape...
-
Circle
must implementDraw()
because it’s abstract -
Display()
is inherited as-is
📌 2. Interfaces
An interface is a pure contract: it only declares **methods, properties, Events, or Indexers but provides no implementation (before C# 8.0; C# 8+ allows default implementation).
-
Classes or structs implement interfaces using
:
-
A class can implement multiple interfaces (solves multiple inheritance limitation)
Syntax
public interface IDrawable
{
void Draw();
void Resize(int factor);
}
Example
public class Circle : IDrawable
{
public void Draw()
{
Console.WriteLine("Drawing a Circle");
}
public void Resize(int factor)
{
Console.WriteLine($"Resizing Circle by {factor}");
}
}
// Usage
IDrawable shape = new Circle();
shape.Draw(); // Drawing a Circle
shape.Resize(2); // Resizing Circle by 2
📌 3. Abstract Class vs Interface
Feature | Abstract Class | Interface |
---|---|---|
Instantiation | Cannot instantiate | Cannot instantiate |
Methods | Can have abstract & concrete | Only declarations (C# 8+ can have default implementations) |
Fields/Properties | Can have fields | No fields (properties only) |
Constructors | Can have | Cannot have |
Inheritance | Single inheritance | Multiple interfaces allowed |
Use Case | Shared base behavior | Define a contract for classes |
📌 4. When to Use
-
Abstract Class:
-
When you want to provide shared behavior and/or fields
-
When there’s a “is-a” relationship
-
-
Interface:
-
When you want to enforce a contract
-
When multiple inheritance of behavior is needed
-
📌 5. Example: Combining Abstract Class and Interface
public interface IMovable
{
void Move();
}
public abstract class Vehicle
{
public string Name { get; set; }
public abstract void Start();
}
public class Car : Vehicle, IMovable
{
public override void Start()
{
Console.WriteLine($"{Name} started");
}
public void Move()
{
Console.WriteLine($"{Name} is moving");
}
}
// Usage
Car car = new Car { Name = "Toyota" };
car.Start(); // Toyota started
car.Move(); // Toyota is moving
Car
inherits shared behavior from Vehicle and implements IMovable contract
✅ Tip:
-
Use abstract classes for common code + structure
-
Use interfaces for polymorphism and contracts
-
You can combine both to get flexibility + code reuse