Challenge: ES6 Inheritance
This challenge will test your skills in implementing inheritance and constructors in ES6 version of JavaScript.
We'll cover the following
Problem statement
In this challenge, there are two classes, Vehicle
and Car
. You need to implement inheritance between them so that Car
is the child class and Vehicle
is the parent. You have the following tasks:
-
Define a
constructor
forVehicle
that initializes afuel
(given in ml) property. -
Define the
turnOn
andturnOff
functions inVehicle
. They should displayTurned on
andTurned off
messages. -
Next, implement inheritance between the
Vehicle
andCar
class. Define theCar
classconstructor
that initializes the parameterisparked
. This is a boolean variable to check whether the car is parked or not. -
Redefine the function
turnOn
inCar
class. If thefuel
is less than500
ml, it should displayRefill Fuel
or it should start the car. -
Redefine the
turnOff
function inCar
class. The car can only turn off if it has been parked; otherwise, it should displayCar not parked
.
Input
Calling turnOn
and turnOff
functions
Output
The message displaying whether the car is turned on, off, needs to refill fuel, or is not parked
Sample input
var car1 = new Car(1000,true)
var car2 = new Car(400,true)
var car3 = new Car(1500,false)
car1.turnOn()
car1.turnOff()
car2.turnOn()
car2.turnOff()
car3.turnOn()
car3.turnOff()
Sample output
"Turned on"
"Turned off"
"Refill Fuel"
"Turned off"
"Turned on"
"Car not parked"
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.