extends vs implements
extends inherits fields and code from a base class, while implements only forces a class to have an interface’s methods and fields, without any of its code.
Side-by-side comparison
#Works between
- extends (interfaces)
- Two classes (or two interfaces)
- implements
- A class and an interface
Inherits code (fields, methods)
- implements
- No, only the shape to implement
How many at once
- extends (interfaces)
- One base class
- implements
- Several interfaces at once
Access to super()
- implements
- Not applicable
A missing method
- extends (interfaces)
- Inherited from the base class
- implements
- Error:
incorrectly implements interface
Typical use
- extends (interfaces)
- Shared logic across derived classes
- implements
- A contract for independent classes
Example
#class Person {
constructor(public name: string) {}
greet(): string {
return `Hi, I am ${this.name}`
}
}
interface Scorable {
score(): number
}
class Student extends Person implements Scorable {
constructor(name: string, private points: number) {
super(name)
}
score(): number {
return this.points
}
}
const student = new Student('Emma', 92)
console.log(student.greet())
console.log(student.score())Hi, I am Emma 92
Definition and usage
#You use both keywords in a class declaration to connect it to another type, and the compiler checks both: if the class doesn’t meet the requirements, it reports an error. A class can extend only one base class, but implement several interfaces at once.
The difference is in what the class gets. extends between classes inherits real code: the fields, methods, and constructor of the base class, which you can call through super() or override. implements inherits no code at all, it only checks at compile time that the class itself has implemented everything the interface promises. extends also works between interfaces, where it extends the list of required fields.
When classes are meant to share common logic, e.g. a shared constructor or a helper method, use extends. When you only want to guarantee that different, unrelated classes have certain methods, e.g. Comparable or Serializable, use implements. Interfaces that describe capabilities combine better than deep class inheritance.
Syntax
#class Derived extends Base { … }
class SomeClass implements SomeInterface { … }More examples
#class Shape {
describe(): string {
return 'A shape'
}
}
class Circle extends Shape {}
const circle = new Circle()
console.log(circle.describe())
console.log(circle instanceof Shape)A shape true
interface Scorable {
score(): number
}
class Exam implements Scorable {}
// Error: Class 'Exam' incorrectly implements interface 'Scorable'.
// Property 'score' is missing in type 'Exam' but required in type 'Scorable'.Good practices
#- One class can implement several interfaces, but extend only one class. If you need several independent “abilities,” reach for interfaces.
implementsonly checks types. You still have to type the method parameters in the class itself, the compiler doesn’t take them over from the interface.- Deep
extendschains are hard to maintain. Composition is often better: a class takes another object as a dependency instead of inheriting from it.
Related entries
#- extends (interfaces)Extending an interface with new fields, based on an existing one.
- implementsA declaration that a class fulfills the contract described by an interface.
- abstract classA base class you can’t create directly, with methods for its descendants to fill in.
- public, private, protectedAccess modifiers that control where a class’s fields and methods can be used from.
- interfaceA named description of an object’s shape that you can extend and merge.
Spotted a mistake or missing an example? Write to us.