Exercise 3: Inheritance with Cars

In this exercise, you need to implement inheritance between two classes, Vehicle, and Car.

Problem Statement

In this exercise, you have to implement inheritance in between classes in ES6 version of JavaScript.

Task 1

The base class Vehicle is declared below. You have to define its constructor. The constructor should contain:

  • Protected values _speed and _model.

  • Methods getModel and getSpeed which return the protected speed and model values.

Task 2

The child class Car is also declared. You have to:

  • Modify its declaration such that it extends the Vehicle class.

  • Define and initialize its constructor.

  • Implement a function setDetails(name) in it which takes a string name and sets it as the name of the Car.

  • Implement a function getDetails(carName) in it which takes a string carName and appends it with the model, name, and speed of the car. It should store the final result in a variable and return it.

Sample Input

getDetails(X)
getDetails(S)
getDetails(Roadster)

Sample Output

X, Tesla, 100
S, Tesla, 100
Roadster, Tesla, 100

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy