Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Class Inheritance

Inheritance Allows you to inherit Properties and Methods from a Parent Class and also Add More, this can make your code Reusable.

Uses of Inheritance

  1. Child Class Inherits properties and methods from parent class, makes your code Reusable. Since you can also add your own functionalities in the child class, you can inherit only the useful functionalities and define other required features.
  2. Once any Functionality is developed, you can simply inherit it, this make code cleaner and easier to maintain.

Declaring Class Inheritance

To Inherit Class, extends keyword is used.

Syntax

// parent class class parentClassName{ constructor(paramters) { //Statements } methodName() { //Statements } } // Child Class inheriting parent class class className extends parentClassName { } let variableName = new className(parameter);

Example

class identity { constructor(name) { this.name = name; } welcomeText(){ console.log("Hello "+ this.name); } } class person extends identity{ age = 20; } let student = new person('John'); student.welcomeText(); console.log(student.age); // Output // Hello John

Here,

We have declared a Parent Class identity with method welcomeText .

We have also declared a child class with name person inherited from identity. and student is an object.

In the child class we have declared age to understand that person class holds its own and its parent class properties and methods.

Super() Keyword

super keyword is used to denote to parent Class. it is used inside child class.

Syntax

// parent class class parentClassName{ constructor(paramters) { //Statements } } // Child Class inheriting parent class class className extends parentClassName { constructor(){ super(parameter); } }

Example

class identity { constructor(name) { this.name = name; } welcomeText(){ console.log("Hello "+ this.name); } } class person extends identity{ constructor(name){ super(name); } } let student = new person('John'); student.welcomeText(); // Output // Hello John

Here,

We have declared a Parent Class identity with method welcomeText .

We have also declared a child class with name person inherited from identity. and student is an object.

In the child class, we have used super because when the constructor of class person is called it will also call the constructor of identity class.

You can also override the predefined methods and properties of parent class by assigning the properties and methods with same name in child class.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?