0
0
Javascriptprogramming~30 mins

Inheritance using classes in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Inheritance using classes
📖 Scenario: Imagine you are creating a simple program to manage vehicles. You want to keep common features in a general vehicle class and add specific features for cars.
🎯 Goal: You will build two classes: a Vehicle class and a Car class that inherits from Vehicle. You will then create a car object and display its details.
📋 What You'll Learn
Create a Vehicle class with a constructor that takes make and year.
Create a Car class that inherits from Vehicle and adds a model property.
Create an instance of Car with specific values for make, year, and model.
Print the car's details using a method.
💡 Why This Matters
🌍 Real World
Inheritance helps organize code by sharing common features in a base class and adding specific features in child classes. This is useful in many programs like games, business apps, and simulations.
💼 Career
Understanding inheritance is important for software development jobs because it helps write clean, reusable, and organized code.
Progress0 / 4 steps
1
Create the Vehicle class
Create a class called Vehicle with a constructor that takes make and year as parameters and assigns them to this.make and this.year.
Javascript
Need a hint?

Use class keyword to create the class and constructor method to set properties.

2
Create the Car class that inherits Vehicle
Create a class called Car that extends Vehicle. Add a constructor that takes make, year, and model. Call super(make, year) and assign model to this.model.
Javascript
Need a hint?

Use extends to inherit and super() to call the parent constructor.

3
Create a Car object
Create a variable called myCar and assign it a new Car object with make as "Toyota", year as 2020, and model as "Corolla".
Javascript
Need a hint?

Use new Car(...) to create the object with the exact values.

4
Print the car details
Add a method called getDetails inside the Car class that returns a string like "Toyota 2020 Corolla" using this.make, this.year, and this.model. Then, print the result of myCar.getDetails().
Javascript
Need a hint?

Use a method that returns a string with all properties and print it with console.log.