🌳 Inheritance

Last Updated: 24th October 2025


Inheritance is the concept of reusing code from one class (parent) in another class (child). It allows the child class to access properties and methods of the parent class.We use super() to call the parent class constructor.

Hinglish Tip 🗣: Jaise aapke ghar ke rules aapko follow karne hote hain, aur bachche un rules ko inherit karte hain. Yaha parent class rules aur methods provide karta hai, child class use reuse karta hai.


Why Use Inheritance?

  1. Reuse existing code → avoid duplication.
  2. Organize code → logical structure with parent-child relationship.
  3. Extend functionality → add new features in child class.

🧩 Syntax

class Parent {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Hello, ${this.name}`);
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name); // call parent constructor
    this.age = age;
  }

  showAge() {
    console.log(`${this.name} is ${this.age} years old`);
  }
}

const c = new Child("Riya", 20);
c.greet(); // ✅ Hello, Riya
c.showAge(); // ✅ Riya is 20 years old

Hinglish Tip 🗣: super(name) ka matlab hai parent ka constructor call karna — bina super ke child constructor me this use nahi kar sakte.


Types of Inheritance

There are following types of inheritance supported in JavaScript:

  • Single Level Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

Single Level Inheritance

there is only one parent class and one child class

class Parent {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Hello, ${this.name}`);
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name); // call parent constructor
    this.age = age;
  }

  showAge() {
    console.log(`${this.name} is ${this.age} years old`);
  }
}

const c = new Child("Riya", 20);
c.greet(); // ✅ Hello, Riya
c.showAge(); // ✅ Riya is 20 years old

Multilevel Inheritance

Chain of inheritance: Grandparent → Parent → Child.

class Grandparent {
  skills() {
    console.log("Gardening");
  }
}

class Parent extends Grandparent {
  hobbies() {
    console.log("Painting");
  }
}

class Child extends Parent {
  play() {
    console.log("Playing");
  }
}

const ch = new Child();
ch.skills(); // Gardening
ch.hobbies(); // Painting
ch.play(); // Playing

Hierarchical inheritance

class Parent {
  skills() {
    console.log("Gardening");
  }
}

class Child1 extends Parent {
  hobbies() {
    console.log("Painting");
  }
}

class Child2 extends Parent {
  play() {
    console.log("Playing");
  }
}

const ch1 = new Child1();
ch1.skills(); // Gardening
ch1.hobbies(); // Painting
const ch2 = new Child2();
ch2.skills(); // Gardening
ch2.play(); // Playing

💡 Quick Practice

  • Create Employee class with salary property and showSalary() method.
  • Create Manager class extending Employee with bonus property and showBonus() method.
  • Override showSalary() in Manager to include bonus.
  • Test objects of both classes.
  • Exercise