Interfaces vs Abstract Classes: When to Use Which
Interfaces define a contract; abstract classes share implementation. Learn the difference, the trade-offs, and how to choose in real designs.
By Velloreth
The core distinction
An interface defines a contract: a set of methods a type must provide. It contains no implementation and no state.
An abstract class defines a contract and can provide implementation and state that subclasses inherit.
// Interface: contract only
interface Shape {
area(): number;
perimeter(): number;
}
// Abstract class: contract plus shared code
abstract class Shape {
abstract area(): number;
abstract perimeter(): number;
describe(): string {
return `Area ${this.area()}, perimeter ${this.perimeter()}`;
}
}
The interface says “anything that can compute an area and perimeter is a Shape”. The abstract class says the same, and additionally gives you describe() for free.
The key structural difference
The most important practical difference is inheritance limits.
| Interface | Abstract class | |
|---|---|---|
| Implementation | None (or defaults) | Yes |
| State (fields) | No | Yes |
| Multiple inheritance | Yes | No (one parent) |
| Constructor | No | Yes |
| Access modifiers | Public only | Any |
| Coupling | Loose | Tight |
A class can implement many interfaces but extend only one class. That single fact drives most design decisions.
class Circle implements Shape, Serializable, Comparable<Circle> {
// satisfies three contracts
}
With abstract classes, you would have to choose one parent and lose the others.
When to use an interface
Unrelated types share a capability. A File, a Socket, and a StringBuilder can all be Closeable. They have nothing else in common, so an interface is the only sensible way to express it.
You want to define a boundary. Interfaces are ideal for describing what a module needs from its dependencies, which is what makes testing with fakes easy.
interface EmailSender {
send(to: string, subject: string, body: string): Promise<void>;
}
class OrderService {
constructor(private email: EmailSender) {}
async placeOrder(order: Order) {
// ...
await this.email.send(order.email, 'Confirmed', '...');
}
}
OrderService depends on the interface, not on a concrete mail client. In tests you pass a fake. In production you pass the real one.
You need multiple contracts. A type that is both Comparable and Serializable needs two interfaces.
You are designing a public API. Interfaces are the smallest possible commitment — they constrain the caller without constraining the implementation.
When to use an abstract class
Related types share implementation. If three shapes all need the same describe() method, an abstract class writes it once.
You need protected members. Interfaces cannot have protected methods or fields. Abstract classes can, which matters when subclasses need access to internals that callers should not see.
You need state. Interfaces cannot hold instance fields. If the shared behaviour depends on shared state, you need an abstract class.
You want to control construction. An abstract class can have a constructor that runs shared initialisation.
You are evolving an API. Adding a method to an interface breaks every implementer. Adding a concrete method to an abstract class does not.
The template method pattern
The classic use of an abstract class is the template method: define the skeleton of an algorithm, and let subclasses fill in the steps.
abstract class DataImporter {
async import(path: string): Promise<number> {
const raw = await this.read(path); // subclass provides
const parsed = this.parse(raw); // subclass provides
const validated = this.validate(parsed); // shared
return this.save(validated); // subclass provides
}
protected abstract read(path: string): Promise<string>;
protected abstract parse(raw: string): unknown[];
protected abstract save(rows: unknown[]): Promise<number>;
protected validate(rows: unknown[]): unknown[] {
return rows.filter((r) => r != null);
}
}
The import method is the template. Subclasses implement the varying steps and inherit the fixed sequence. This is genuinely useful when the algorithm’s structure is stable and only the details vary.
Why modern design prefers interfaces
The shift away from abstract classes is about coupling.
Inheritance creates a dependency on implementation details. A subclass inherits not just the contract but the behaviour, and it can break when the parent changes. This is the fragile base class problem: a change to a base class that seems safe breaks subclasses in ways the compiler cannot detect.
Composition avoids this. Instead of inheriting behaviour, you hold a reference to an object that provides it.
// Inheritance: tightly coupled
class Logger { log(msg: string) { console.log(msg); } }
class UserService extends Logger { /* inherits log */ }
// Composition: loosely coupled
interface Logger { log(msg: string): void; }
class UserService {
constructor(private logger: Logger) {}
}
The composition version can swap the logger, test with a fake, and change the logging strategy without touching UserService. The inheritance version is stuck with Logger as a parent.
This is why the advice “prefer composition over inheritance” appears in almost every design guide. Interfaces plus composition give you the reuse without the coupling.
The line has blurred
Modern languages have narrowed the gap.
Java 8+ allows default methods in interfaces:
interface Shape {
double area();
default String describe() {
return "Area: " + area();
}
}
C# 8+ has default interface methods.
Rust has traits, which can provide default implementations and are the primary abstraction mechanism.
Go has interfaces that are satisfied implicitly — a type implements an interface just by having the right methods, with no declaration.
Swift has protocols with extensions that provide default implementations.
The practical consequence: in many languages you can now get shared implementation without inheritance, which removes one of the main reasons to use an abstract class.
What interfaces still cannot do is hold instance state. If the shared behaviour needs fields, you need an abstract class or a composition-based approach.
A decision guide
| Situation | Use |
|---|---|
| Unrelated types share a capability | Interface |
| You need multiple contracts on one type | Interface |
| You are defining a dependency boundary | Interface |
| Related types share implementation | Abstract class |
| You need protected members or state | Abstract class |
| You want to evolve an API without breaking implementers | Abstract class |
| You want maximum flexibility and testability | Interface + composition |
The default in modern code is interfaces. Reach for an abstract class when you have a genuine family of types with shared implementation and state, and when the coupling is acceptable.
Common mistakes
Using an abstract class for a single implementation. If there is only one subclass, the abstraction is not earning its keep.
Deep inheritance hierarchies. Three or four levels of inheritance are hard to follow. Prefer composition.
Interfaces with one method that is never implemented differently. If every implementer does the same thing, the interface is not abstracting anything.
Adding methods to a published interface. This breaks every implementer. Use a new interface, or a default method where the language supports it.
Confusing “is-a” with “has-a”. A Car has an Engine; it is not an Engine. Inheritance is for is-a relationships, and even then composition is often better.
Abstract classes with no abstract methods. If nothing is abstract, it is just a base class, and the abstract keyword is misleading.
Practical guidance
- Default to interfaces for contracts and boundaries.
- Use abstract classes for genuine families with shared implementation and state.
- Prefer composition over inheritance for reuse.
- Keep hierarchies shallow — one level of abstraction is usually enough.
- Design interfaces around capabilities, not around types.
- Remember that adding to an interface is a breaking change.
- Test through the interface, not the implementation.
For the related design principles, see SOLID principles explained and composition vs inheritance. For the practical application, dependency injection without a framework and design patterns for beginners.
Quotable summary
An interface defines a contract with no implementation or state, while an abstract class can provide both a contract and shared code. A class can implement many interfaces but extend only one class, which makes interfaces the more flexible tool for describing capabilities. Use an interface when unrelated types share a capability or when you are defining a dependency boundary; use an abstract class when related types share implementation and state. Inheritance creates coupling to implementation details, which is why modern design favours interfaces plus composition. Default methods in Java and C#, traits in Rust, and protocol extensions in Swift have blurred the line, but interfaces still cannot hold instance state.