Inheritance

by REPLY2SAGAR

JavaScript

class ElectricCar extends Car {
    constructor(brand, model, batteryCapacity) {
        super(brand, model);  // Call the parent class constructor
        this.batteryCapacity = batteryCapacity;
    }

    display() {
        super.display(); // Call the display method from Car
        console.log(`It has a battery capacity of ${this.batteryCapacity} kWh.`);
    }
}

// Creating an instance of ElectricCar
let myTesla = new ElectricCar("Tesla", "Model S", 85);
myTesla.display();  // Outputs: This car is a Tesla Model S. It has a battery capacity of 85 kWh.