Roadmap › OOP Concepts in C# › Module: Interfaces and Abstract Classes
Lessons in this course (15)
9. Interface vs abstract class
Intermediate★ Must knowLesson 9 of 15
| Interface | Abstract class | |
|---|---|---|
| Purpose | A contract / capability ("can do") | A partial base implementation ("is a") |
| Multiple | A class can implement many | A class can inherit only one |
| Fields / state | No instance fields | Can have fields and state |
| Constructors | No | Yes |
| Implementation | Default methods allowed since C# 8 | Abstract and concrete members |
| Access modifiers | Members public by default | Any |
| Use when | Unrelated types share behaviour; for DI and mocking | Related types share code and state |
csharp
public interface IShape { double Area(); string Name => GetType().Name; } // default implementation (C# 8)
public abstract class Shape : IShape
{
public abstract double Area();
public override string ToString() => $"{GetType().Name} area={Area():F2}"; // shared code
}
public class Circle(double r) : Shape { public override double Area() => Math.PI * r * r; }