Abstract Classes vs Interfaces
You'll learn: How to decide between abstract classes and interfaces based on whether you need shared implementation or just contracts.
The Core Decision
Both abstract classes and interfaces define the shape your types must follow, but they solve different problems:
Interfaces are pure contracts — they only describe what properties and methods must exist, without providing any implementation. They're compile-time only and completely disappear after TypeScript compiles.
Abstract classes can provide both contracts (abstract methods) and shared implementation (concrete methods). They exist at runtime and support common logic across child classes.
When to Choose Interfaces
Use interfaces when you only need to enforce a contract:
interface Logger {
log(message: string): void;
error(message: string): void;
}
class ConsoleLogger implements Logger {
log(message: string) {
console.log(message);
}
error(message: string) {
console.error(message);
}
}
Interfaces are ideal when multiple unrelated classes need the same structure but implement it completely differently.
When to Choose Abstract Classes
Use abstract classes when you need shared implementation:
abstract class Logger {
protected timestamp(): string {
return new Date().toISOString();
}
abstract log(message: string): void;
}
class FileLogger extends Logger {
log(message: string) {
// Can use inherited timestamp()
console.log(`${this.timestamp()}: ${message}`);
}
}
Here, timestamp() is shared logic that all loggers can reuse. You couldn't do this with an interface.
The Trade-off
Interfaces are more flexible (classes can implement many interfaces) but provide zero code reuse. Abstract classes enable shared implementation but classes can only extend one abstract class.
Key Takeaway: Use interfaces for pure type contracts; use abstract classes when you need to share implementation logic alongside abstract requirements.