9. Javascript ES6: Inheritance

In this section we will learn about ES6: inheritance.

To create an inheritance we use extends keyword. A class created with class inheritance inherits all the methods from the base class.

   

Classes support prototype-based inheritance, super calls, instance and static methods and constructors.

The Super() method refers to the parent class 

By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods.

Inheritance is useful for code re usability: reuse the properties and methods of an existing class when you create a class.

We can overwrite the constructor in the sub class.

   
    class Person {
        constructor(name, age) {
            this.name = name;
            this.age  = age;
        }
        getMyName(){
            return this.name;
        }
    }
    class Student extends Person {
        constructor(name,age) {
            super(name,age);
            this.name = name;  
        }
    }
    const me = new Student('Prabakaran', 30);
    console.log("My Name is: ", me);
 

 

We can reuse the properties & methods in subclass from the base class.









Comments