Side by side
Abstract ClassvsInterface
What is the difference between an abstract class and an interface?
Updated 3 min read8 differences
In short
An abstract class shares code and state with its subclasses; an interface only declares what a type can do, and a class can implement many.
Abstract Class
An abstract class is a class that can't be instantiated on its own; it shares code with its subclasses and declares methods that each subclass must implement.
Read the page on Abstract ClassInterface
An interface is a named set of method and property signatures that a type promises to provide, without saying how those members are implemented.
Read the page on InterfaceAbstract Class and Interface compared
| Aspect | Abstract Class | Interface |
|---|---|---|
| Purpose | Shares code and state among related classes | Describes what a type can do |
| Per class | One (in Java, C# and TypeScript) | As many as needed |
| Methods | Abstract methods plus fully written ones | Signatures; default bodies since Java 8 and C# 8 |
| Fields | Instance fields with their own values | No instance state; at most constants or property declarations |
| Constructor | Yes, called by subclasses through super | No |
| Member access | Can use protected and private members | Members are public by default |
| Relationship | is-a: a Circle is a Shape | can-do: a Circle is Printable |
| In TypeScript at runtime | Compiles to a real JavaScript class | Erased; exists only for type checking |
The difference, explained
An abstract class is a class that can't be instantiated on its own; it exists to be extended. It can hold fields, a constructor and fully written methods, next to abstract methods that have no body and must be written by each subclass. An interface is a named contract: a list of method signatures, and sometimes properties, that a type promises to provide, with no state of its own.
The essential difference is that an abstract class shares how related types work, while an interface describes what a type can do. In Java, C# and TypeScript a class can extend only one class but implement many interfaces, so an abstract class fixes a type's place in one family, while interfaces can be combined freely: a Circle can be both HasArea and Printable. Abstract classes suit the template method pattern, where a base class writes describe() once and leaves area() to each subclass.
The two often work together. A common design defines the contract as an interface and adds an abstract class that implements part of it as a convenient base, as Java does with the List interface and the AbstractList class. Other code depends only on the interface, so any implementation, including a fake in tests, can be passed in, while related implementations still share code through the abstract class.
A common misconception is that the difference disappeared when Java 8 and C# 8 let interfaces contain default method bodies. Instance fields and constructors still belong only to classes, and a class can still extend just one. Languages also differ: TypeScript interfaces vanish after compilation, while Python has no interface keyword and uses abstract base classes from abc or typing.Protocol instead.
Which one should you use?
Choose Abstract Class when…
- Related classes share real code or fields, not just method names.
- A base class should fix the steps of an algorithm and let subclasses fill some in.
- Subclasses need a shared constructor or
protectedhelpers.
Choose Interface when…
- Unrelated classes should offer the same ability, such as being comparable or printable.
- A class needs to fulfill several contracts at once.
- You want to swap implementations, for example a fake in tests.
- You only need to describe the shape of an object or an API.
Shapes in TypeScript: one abstract base class, or several interfaces
// Abstract class: shared code, plus gaps each subclass fills
abstract class Shape {
constructor(public readonly name: string) {}
abstract area(): number; // no body
describe() { return `${this.name}: ${this.area().toFixed(1)}`; }
}
class Circle extends Shape { // extends exactly one class
constructor(private r: number) { super("circle"); }
area() { return Math.PI * this.r ** 2; }
}// Interfaces: contracts only, and a class can take several
interface HasArea { area(): number; }
interface Printable { print(): string; }
class Circle implements HasArea, Printable {
constructor(private r: number) {}
area() { return Math.PI * this.r ** 2; }
print() { return `circle: ${this.area().toFixed(1)}`; }
}
// Any object with the right shape fits, class or not
const square: HasArea = { area: () => 4 };Readers ask
Can a class extend an abstract class and implement interfaces at the same time?
Yes. In Java or TypeScript you write class Circle extends Shape implements Printable: one base class, plus any number of interfaces.
If interfaces can have default methods, why use an abstract class?
Because interfaces still can't hold instance fields or constructors. When related classes share state, setup logic or protected helpers, an abstract class is the place for it.
Does Python have interfaces?
Not as a keyword. Abstract base classes from the abc module play that role, and typing.Protocol describes interfaces that type checkers match by shape, as in TypeScript and Go.