How Do I Create Class Inheritance With Java Script

admin_img Posted By Bajarangi soft , Posted On 09-10-2020

In Java Script we can create a class inheritance, use the extends keyword.so today we create how to create class inheritance

How Do I Create Class Inheritance With Java Script

Class Definition

Use the keyword class to create a class, and always add the constructor() method.

The constructor method is called each time the class object is initialized.

Example A simple class definition for a class named "Car":

class Car {
    constructor(brand) {
        this.carname = brand;
    }
    static hello() {
        return "Hello!!";
    }
}

Now you can create objects using the Car class:

<script>
    class Car {
        constructor(brand) {
            this.carname = brand;
        }
    }

    mycar = new Car("Ford");

    document.getElementById("demo").innerHTML = mycar.carname;
</script>


Inheritance

To create a class inheritance, use the extends keyword.

A class created with a class inheritance inherits all the methods from another class:
 

Related Post