Hint
extends = inherit implementation (IS-A); implements = satisfy a contract (CAN-DO); class can implement multiple interfaces
extends — inherits from a parent class or interface. Gets all properties, methods, and constructor logic.
implements — declares that a class satisfies an interface contract, but does NOT inherit implementation.
// Interface contract
interface Serializable {
serialize(): string;
}
interface Loggable {
log(): void;
}
// extends — inherits implementation
class Animal {
constructor(public name: string) {}
breathe() { return 'breathing'; }
}
class Dog extends Animal {
// Inherits name, breathe() from Animal
bark() { return 'woof'; }
}
// implements — satisfy contract without inheriting
class User implements Serializable, Loggable {
constructor(public name: string, public email: string) {}
// MUST implement all interface methods
serialize() { return JSON.stringify({ name: this.name, email: this.email }); }
log() { console.log(this.name); }
}
// Combining both
class AdminUser extends User implements Comparable<AdminUser> {
constructor(name: string, email: string, public adminLevel: number) {
super(name, email); // must call parent constructor
}
compareTo(other: AdminUser): number {
return this.adminLevel - other.adminLevel;
}
}
Interface implements itself (structural check):
// A class can implement an interface to get compile-time checking
// that it fulfills the contract — even without runtime cost
class Repository implements IRepository<User> {
// TypeScript errors if any method is missing
}
implements if the class already has the right shape. But writing it explicitly documents intent and gives clearer error messages when the contract isn't met.