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 the 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) which takes a string name and sets it as the name of the Car.

  • Implement a function getDetails(carName) which takes a string carName and appends it with the model 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

Get hands-on with 1200+ tech skills courses.