01 / 21

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.

JavaScript

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.

Parent Class
The base class that defines shared properties and methods.
Child Class
A new class that inherits from the parent and can add or override behavior.

Why Use Inheritance?

1
Code Reuse: Write shared logic once in the parent. Every child gets it for free.
2
Organization: Group related classes into clear hierarchies that model real-world relationships.
3
Extensibility: Add new child classes without modifying the parent, keeping existing code stable.
4
Polymorphism: Children can override parent methods to provide specialized behavior while sharing the same interface.

Visualizing the Class Hierarchy

Animal name, speak() Dog breed, fetch() Cat indoor, purr() Bird wingspan, fly() extends extends extends All children inherit name and speak() from Animal Each adds its own unique properties and methods

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
  }
}
super(args)
Calls the parent constructor. Required in the child constructor before accessing this.
super.method()
Calls a parent method from inside a child method, useful when overriding.

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

1
Define the parent class with a constructor and shared methods.
2
Declare the child class using class Child extends Parent.
3
Write the child constructor if the child needs its own properties.
4
Call super() as the first line inside the child constructor, passing any arguments the parent needs.
5
Add child-specific properties after super(), and add or override methods below the constructor.

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

Object.prototype Animal.prototype speak() Dog.prototype fetch() [[Prototype]] [[Prototype]] Method lookup travels UP the chain until found

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

Forgetting super()
If your child class has a constructor but does not call super(), JavaScript throws a ReferenceError. You cannot use this before super().
Using this Before super()
Accessing this.name before calling super() causes an error because the object is not yet initialized.
Missing Arguments
Calling super() without passing the parent's required arguments leaves inherited properties as undefined.

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

What does c.describe() return?

Question 4 of 5

What is super.method() used for inside a child class?

Question 5 of 5

What does t.honk() return, and why?