Challenge: Implement Class Inheritance with extends and super()
Task
You're working on a transportation management system that tracks different types of vehicles. Each vehicle has a make, model, and year. For electric vehicles, you also need to track the batteryCapacity. You'll use inheritance to extend a general Vehicle class for specific electric vehicles.
- Create an
ElectricVehicleclass that extendsVehicle; - The
ElectricVehicleconstructor should acceptmake,model,year, andbatteryCapacity; - Use
super()to initializemake,model, andyearin the parent class; - Add a
getDetailsmethod toElectricVehiclethat overridesVehicle'sgetDetailsmethod. It should callsuper.getDetails()and add information aboutbatteryCapacity. The format should be:"{make} {model} ({year}) with a battery capacity of {batteryCapacity} kWh.".
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle _____ _____ { _____(make, model, year, _____) { _____(_____, _____, _____); this.batteryCapacity = batteryCapacity; } _____() { return `${super._____} with a battery capacity of ${ _____._____ } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Expected: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Expected: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
- Define a class
ElectricVehiclethat extendsVehicle; - In the
ElectricVehicleconstructor, add a parameterbatteryCapacityaftermake,model, andyear; - Use
super(make, model, year)to call the parent class's constructor and initializemake,model, andyear; - Assign
batteryCapacityto a property inElectricVehicle; - Define a
getDetailsmethod inElectricVehiclethat overridesVehicle'sgetDetailsmethod; - Inside
getDetails, usesuper.getDetails()to get the parent class details, then add the battery capacity information to the return string.
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle extends Vehicle { constructor(make, model, year, batteryCapacity) { super(make, model, year); this.batteryCapacity = batteryCapacity; } getDetails() { return `${super.getDetails()} with a battery capacity of ${ this.batteryCapacity } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Output: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Output: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how inheritance works in this example?
What does the super() function do in the constructor?
How does method overriding work in JavaScript classes?
Awesome!
Completion rate improved to 2.22
Challenge: Implement Class Inheritance with extends and super()
Swipe to show menu
Task
You're working on a transportation management system that tracks different types of vehicles. Each vehicle has a make, model, and year. For electric vehicles, you also need to track the batteryCapacity. You'll use inheritance to extend a general Vehicle class for specific electric vehicles.
- Create an
ElectricVehicleclass that extendsVehicle; - The
ElectricVehicleconstructor should acceptmake,model,year, andbatteryCapacity; - Use
super()to initializemake,model, andyearin the parent class; - Add a
getDetailsmethod toElectricVehiclethat overridesVehicle'sgetDetailsmethod. It should callsuper.getDetails()and add information aboutbatteryCapacity. The format should be:"{make} {model} ({year}) with a battery capacity of {batteryCapacity} kWh.".
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle _____ _____ { _____(make, model, year, _____) { _____(_____, _____, _____); this.batteryCapacity = batteryCapacity; } _____() { return `${super._____} with a battery capacity of ${ _____._____ } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Expected: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Expected: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
- Define a class
ElectricVehiclethat extendsVehicle; - In the
ElectricVehicleconstructor, add a parameterbatteryCapacityaftermake,model, andyear; - Use
super(make, model, year)to call the parent class's constructor and initializemake,model, andyear; - Assign
batteryCapacityto a property inElectricVehicle; - Define a
getDetailsmethod inElectricVehiclethat overridesVehicle'sgetDetailsmethod; - Inside
getDetails, usesuper.getDetails()to get the parent class details, then add the battery capacity information to the return string.
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle extends Vehicle { constructor(make, model, year, batteryCapacity) { super(make, model, year); this.batteryCapacity = batteryCapacity; } getDetails() { return `${super.getDetails()} with a battery capacity of ${ this.batteryCapacity } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Output: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Output: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
Thanks for your feedback!