Inheritance in javaScript

 

In this article going to show what exactly inheritance and implementation in javaScrip

In a simple word inheritance is the procedure in which one class inherits the attributes and methods of another class.

Example

class Car {
  constructor(name, model) {
    this.name = name;
    this.model = model;
  }

  startEngine() {
    console.log(`${this.name}'s engine has been started`);
  }

  stopEngine() {
    console.log(`${this.name}'s engine has been stoped`);
  }

}

class Tesla extends Car {

  constructor(name, model, speed) {

    super(name, model);

    this.speed = speed;
  }

  carSpeed() {
    console.log(`${this.name} speed is now ${this.speed}`);
  }

}

const ob = new Tesla(`Nikola Tesla`, `Tesla Model S`, 250);
ob.startEngine();
ob.carSpeed();
ob.stopEngine();

 

About Author

Photo

Hi, I am Mahbub Hasan
Software Engineer

I am very much interested to share my programming
and development knowledge with the people.I will try
to update my blog everyday with new technology inshallah.

Thank You

For Visiting My Blog.

Contact Me