
Inheritance in JavaScript
In JavaScript, inheritance means that one class (child) can use the properties and methods of another class (parent).Itβs a way to reuse code and create a logical relationship between objects. We use the keyword extends in classes and super() to call the parentβs constructor.
Example of Inheritance
class Vehicle {
move() {
console.log("This vehicle is moving...");
}
stop() {
console.log("This vehicle has stopped.");
}
}
// Child class inheriting Vehicle
class Car extends Vehicle {
honk() {
console.log("Car says: Beep Beep!");
}
}
// Using it
let myCar = new Car();
myCar.move(); // from Vehicle
myCar.honk(); // from Car
myCar.stop(); // from Vehicle