Javascript class inheritance

Extending a Class

Inheritance is a mechanism by witch one object acquires all the properties and behaviors of a parent object. But to understand the concept of class you need to understand the concept of Class. Mainly we will use these term when we are using Inheritance.

1. Sub Class or Child Class: Class which inherits the other class.
2. Super Class or Parent Class: Class from where a subclass inherits the features.

extends keyword play an important role to inherit the properties of parent class.


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

    getName() {
        return this.name;
    }
}

class BE extends Student {
    constructor(name, clg) {
        super(name);
        this.clg = clg;
    }

    getClg() {
        return this.clg;
    }
}


var stu = new BE("xyz", "abc");
stu.getName(); // xyz
stu.getClg(); // abc

You May Also Like

About the Author: Pankaj Bisht