How does implements differ from extends in classes?
Short answer
| Keyword | What it does | Relation |
|---|---|---|
extends | Inherits implementation and state from the parent class | "is a kind of" (is-a) |
implements | Requires a class to match an interface (type), but does not inherit code | "matches the shape" (conforms-to) |
1. extends - Inheriting behavior
A class with
extendsinherits the parent's fields and methods. It can override them and call them throughsuper.
Example:
class Animal {
move() {
console.log("Animal moves");
}
}
class Dog extends Animal {
bark() {
console.log("Woof!");
}
}
const dog = new Dog();
dog.move(); // inherited from Animal
dog.bark(); // its own methodDog inherits the implementation from Animal.
This means Dog already knows how to move, without rewriting the move method.
2. implements - Implementing a contract (interface)
A class with
implementsgets nothing "for free". It is simply required to implement all the properties and methods listed in the interface.
Example:
interface Logger {
log(message: string): void;
}
class ConsoleLogger implements Logger {
log(message: string) {
console.log(message);
}
}
const logger = new ConsoleLogger();
logger.log("Hello!"); // implemented by handHere Logger is a contract,
and ConsoleLogger commits to implementing all its methods.
3. The main difference in one sentence
extends-> inherits implementation and state (code, fields, methods).implements-> guarantees the presence of methods and properties (a contract), but you write the implementation yourself.
4. You can combine them
You can inherit from one class and implement several interfaces at the same time:
interface Swimmer {
swim(): void;
}
interface Runner {
run(): void;
}
class Animal {
eat() {}
}
class Human extends Animal implements Swimmer, Runner {
swim() {
console.log("Swimming");
}
run() {
console.log("Running");
}
}
const person = new Human();
person.eat(); // from Animal
person.swim(); // from Swimmer
person.run(); // from Runnerextendsallows only one (class),implementsallows many (interfaces).
5. extends for classes vs interfaces
TypeScript allows extends on interfaces too,
but there it works a bit differently.
| Context | What extends does |
|---|---|
| On classes | Inherits implementation and state |
| On interfaces | Merges (inherits) signatures, creating a new type |
Example:
interface A {
a: number;
}
interface B extends A {
b: string;
}
const obj: B = { a: 1, b: "hi" }; // merged properties6. You cannot use implements with an interface inside an interface
Interfaces can extend each other (extends),
but they cannot "implement", implements is meant only for classes.
// Error:
interface X implements Y {}7. Multiple inheritance (and why it is forbidden)
In TypeScript (as in most OOP languages, except C++) you cannot inherit from several classes at once, because this leads to implementation conflicts.
class A {}
class B {}
// Error
class C extends A, B {}But you can implement several interfaces:
interface X {}
interface Y {}
class C implements X, Y {}8. A real example: a shared interface and a concrete implementation
interface Repository {
save(data: object): void;
findById(id: number): object;
}
class UserRepository implements Repository {
save(data: object) {
console.log("Saved", data);
}
findById(id: number) {
return { id, name: "Alex" };
}
}UserRepository is required to implement the save and findById methods,
but can implement them however it wants.
9. Using extends in classes with super
class Vehicle {
constructor(public speed: number) {}
move() {
console.log(`Moving at ${this.speed} km/h`);
}
}
class Car extends Vehicle {
constructor(speed: number, public brand: string) {
super(speed); // calls the Vehicle constructor
}
move() {
super.move(); // calls the parent's method
console.log(`${this.brand} is driving`);
}
}
const car = new Car(100, "BMW");
car.move();
// Moving at 100 km/h
// BMW is drivingextends means inheriting behavior plus the ability to override it.
10. When to use which
| Goal | What to choose | Why |
|---|---|---|
| You want to reuse existing code (methods, fields) | extends | You inherit the implementation |
| You want to define a mandatory contract (API, interface) | implements | The class implements the details itself |
| You want a shared shape for several interfaces | extends (between interfaces) | You merge the signatures |
| You want to combine behavior + a contract | extends + implements | Typical for complex hierarchies |
Summary
| Keyword | For whom | What it does | Inherits implementation? | Can you use several? |
|---|---|---|---|---|
| extends | classes / interfaces | Inherits behavior (or signatures) | Yes (for classes) | Only one class |
| implements | classes only | Checks conformance to an interface | No | Yes |
In simple terms:
extendsmeans "inherit everything it has".implementsmeans "do the same thing in shape, but with your own hands".
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.