Intermediate · 13 of 16 · 15 min read
implements and extends
One inherits code and one only checks a shape — what each costs, why a class can implement many interfaces, and what implements does not do.
- Classes
extends gives you somebody else’s implementation. implements gives you a compile-time check
and no code at all. They look similar in a class header and do entirely different jobs.
Side by side
class Animal {
constructor(public name: string) {}
move() { return `${this.name} moves`; }
}
interface Flyable { fly(): string }
interface Swimmable { swim(): string }
class Dog extends Animal {} // inherits move()
class Duck extends Animal implements Flyable, Swimmable {
fly() { return `${this.name} flies`; } // must be written
swim() { return `${this.name} swims`; }
}
console.log(new Dog("Rex").move()); // "Rex moves" — inherited
console.log(new Duck("Donald").fly()); // "Donald flies" — implemented
Dog wrote no code and gained a method. Duck wrote both methods and gained a check that it did.
extends |
implements |
|
|---|---|---|
| Inherits implementation | yes | no |
| How many | one | any number |
| Target | a class (or an interface extending interfaces) | an interface or object type |
| Emitted JavaScript | yes | none |
| Members typed for you | yes | no |
| Constructor | super() required |
nothing |
| Use for | reusing behaviour | enforcing a shape |
implements only checks
The common surprise: the clause verifies the class, it does not supply any types.
interface Person {
name: string;
greet(greeting: string): void;
}
class Student implements Person {
// name; // implicitly any — the interface does not type it
constructor(public name: string) {}
greet(greeting: string) { // the parameter still needs its annotation
console.log(`${greeting}, ${this.name}`);
}
}
new Student("Ana").greet("Hi"); // "Hi, Ana"
A one-way audit: if the class is missing a member or gets one wrong, the clause reports it. It never infers anything into the class.
Leaving the clause off entirely changes nothing about assignability, because the type system is structural:
interface Person { name: string }
class Declared implements Person {
constructor(public name: string) {}
}
class Undeclared {
constructor(public name: string) {}
}
function greet(person: Person) { return person.name; }
console.log(greet(new Declared("Ana")), greet(new Undeclared("Beto")));
Both work. implements is documentation plus an early error — the error appears at the class
rather than at the first call site that fails.
Interfaces extending interfaces
extends is not only for classes:
interface Entity { id: number }
interface Timestamped { createdAt: Date }
interface User extends Entity, Timestamped {
name: string;
}
const user: User = { id: 1, createdAt: new Date(0), name: "Ana" };
console.log(user.id, user.name);
Multiple extension is allowed here, unlike class inheritance. An interface can also extend a class, which copies its instance shape including private members — a rare but real trick for saying “something shaped exactly like this class”:
class Widget {
private internal = 1;
render() { return "x"; }
}
interface WidgetLike extends Widget {}
// Only a Widget or a subclass can satisfy it, because of the private member.
const w: WidgetLike = new Widget();
console.log(w.render());
Abstract classes: both at once
When you want shared code and required members, an abstract class does both:
abstract class Shape {
constructor(public name: string) {}
abstract area(): number; // the hole
describe(): string { // the shared part
return `${this.name}: ${this.area()}`;
}
}
class Square extends Shape {
constructor(private side: number) { super("square"); }
area() { return this.side ** 2; }
}
console.log(new Square(3).describe()); // "square: 9"
// new Shape("x"); // Cannot create an instance of an abstract class
| interface | abstract class | |
|---|---|---|
| Shared implementation | no | yes |
| Constructor and state | no | yes |
| How many per class | any number | one |
| Exists at runtime | no | yes |
| Can be instantiated | n/a | no |
Prefer the interface when all you need is a contract: it costs nothing at runtime and does not consume the inheritance slot. Reach for the abstract class when there is genuinely shared code that subclasses should not rewrite.
super
class Base {
constructor(public id: number) {}
describe() { return `#${this.id}`; }
}
class Derived extends Base {
constructor(id: number, public name: string) {
super(id); // required before `this`, and the compiler enforces it
}
describe() {
return `${super.describe()} ${this.name}`; // extend rather than replace
}
}
console.log(new Derived(1, "Ana").describe()); // "#1 Ana"
The compiler enforces what JavaScript enforces at runtime: super() first, then this. An
override must also stay substitutable — it may accept more and return less, never the reverse:
class Repo {
find(id: number | string): object | null { return null; }
}
class Narrowed extends Repo {
// find(id: number): object | null { return null; }
// 'string | number' is not assignable to 'number'
find(id: number | string | boolean): { ok: true } { return { ok: true }; }
// wider parameter, narrower return — allowed
}
console.log(new Narrowed().find(1));
That rule is Liskov substitution, mechanised by the variance checker.
override
With noImplicitOverride on, an override has to say so — which catches the rename:
class Base {
describe() { return "base"; }
}
class Derived extends Base {
override describe() { return "derived"; }
// override descrbe() {} // This member cannot have an 'override' modifier
// // because it is not declared in the base class.
}
console.log(new Derived().describe()); // "derived"
Without it, renaming describe in the base silently turns every override into a new, unused
method.
Choosing
- Reusing real behaviour, and the relationship is stable “is-a” →
extends. - Enforcing a shape, especially more than one →
implements. - Both, and the hierarchy is yours → an abstract class.
- Neither, and you want to compose capabilities → plain functions and objects, which is the composition answer and usually the right one past two levels of inheritance.
Common mistakes
Expecting implements to give you the member types
Mistake. Declaring name; in a class that implements an interface with name: string.
Why it fails. The clause checks; it does not infer. The field is an implicit any.
What to do instead. Annotate every member — or use a parameter property.
Using extends to share a few helpers
Mistake. A BaseService every service extends for three utility methods.
Why it fails. It couples unrelated classes, consumes the one inheritance slot, and grows
until nobody knows what is inherited.
What to do instead. Import the helpers as functions, or compose them in.
Overriding without override
Mistake. Relying on the method name matching the base.
Why it fails. A rename in the base leaves an orphan method that is never called, and nothing
reports it.
What to do instead. Turn on noImplicitOverride and write the keyword.
Check yourself
3 questions from the bank on what you just read. Answering here does not affect your quiz history.
What is the difference between extends and implements on a class?
An implements clause checks the class, but it does not type the class members for you.
Why is name implicitly any here?
interface Person {
name: string;
greet(): void;
}
class Student implements Person {
name;
greet() {}
}What does an abstract class give you that an interface does not?
Further reading
Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.
Reaching the end of a lecture marks it as read.