Inheritance with
extends & super
Learn how JavaScript classes can inherit properties and methods from parent classes using the extends keyword, and how super calls the parent constructor.
What is Inheritance?
Inheritance is a core principle of object-oriented programming. It lets a new class reuse the code of an existing class: all the properties and methods of the parent automatically become available in the child.
Why Use Inheritance?
Visualizing the Class Hierarchy
The extends Keyword
The extends keyword creates a child class that inherits from a parent class. The child automatically gains access to all of the parent's methods and properties.
class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a sound.`; } } class Dog extends Animal { // Dog inherits constructor and speak() }
The super Keyword
When a child class has its own constructor, it must call super() before using this. The super() call runs the parent's constructor, initializing inherited properties.
class Dog extends Animal { constructor(name, breed) { super(name); // calls Animal's constructor this.breed = breed; // Dog's own property } }
extends vs super: Key Distinction
| extends | super | |
|---|---|---|
| Purpose | Creates inheritance relationship | Calls parent constructor or method |
| Where used | Class declaration line | Inside child constructor or methods |
| Required? | Yes, for any inheritance | Yes, if child has a constructor |
| What it does | Links prototype chain | Runs parent code in child context |
| Analogy | Filling out adoption papers | Calling your parent for advice |
How to Apply: Step-by-Step
Example 1: Basic Inheritance
class Vehicle { constructor(make, year) { this.make = make; this.year = year; } info() { return `${this.year} ${this.make}`; } } class Car extends Vehicle { constructor(make, year, doors) { super(make, year); this.doors = doors; } } const myCar = new Car("Toyota", 2024, 4); myCar.info(); // "2024 Toyota" myCar.doors; // 4
Example 2: Method Overriding
class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a sound.`; } } class Cat extends Animal { speak() { return `${this.name} meows.`; } } const kitty = new Cat("Luna"); kitty.speak(); // "Luna meows."
Example 3: Calling super.method()
class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a sound.`; } } class Dog extends Animal { speak() { const base = super.speak(); return `${base} Specifically, barks!`; } } const rex = new Dog("Rex"); rex.speak(); // "Rex makes a sound. Specifically, barks!"
Under the Hood: Prototype Chain
Checking with instanceof
The instanceof operator lets you verify if an object belongs to a class or any of its ancestors in the inheritance chain.
const rex = new Dog("Rex", "Labrador"); rex instanceof Dog; // true rex instanceof Animal; // true rex instanceof Object; // true rex instanceof Cat; // false
Multi-Level Inheritance
class Animal { constructor(name) { this.name = name; } } class Dog extends Animal { constructor(name, breed) { super(name); this.breed = breed; } } class ServiceDog extends Dog { constructor(name, breed, task) { super(name, breed); this.task = task; } } const helper = new ServiceDog("Max", "Retriever", "guide"); // helper.name: "Max", helper.breed: "Retriever", helper.task: "guide"
Common Mistakes
Summary Reference Table
| Concept | Syntax | Purpose |
|---|---|---|
| Declare inheritance | class Child extends Parent | Links prototype chain |
| Call parent constructor | super(args) | Initializes inherited properties |
| Call parent method | super.method() | Reuse parent logic in override |
| Override a method | Same method name in child | Replace parent behavior |
| Check type | obj instanceof Class | Verify inheritance relationship |
Question 1 of 5
What does the extends keyword do in JavaScript?
Question 2 of 5
What happens if a child class constructor does not call super()?
Question 3 of 5
class Shape {
constructor(color) { this.color = color; }
describe() { return `A ${this.color} shape`; }
}
class Circle extends Shape {
constructor(color, radius) {
super(color);
this.radius = radius;
}
area() { return Math.PI * this.radius ** 2; }
}
const c = new Circle("red", 5);
What does c.describe() return?
Question 4 of 5
What is super.method() used for inside a child class?
Question 5 of 5
class Vehicle {
constructor(make) { this.make = make; }
honk() { return "Beep!"; }
}
class Truck extends Vehicle {
constructor(make, payload) {
super(make);
this.payload = payload;
}
honk() { return "HONK!"; }
}
const t = new Truck("Ford", 2000);
What does t.honk() return, and why?