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();