0
0
Javascriptprogramming~15 mins

Method overriding in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Method overriding
📖 Scenario: Imagine you have a basic Vehicle class that can describe a vehicle's sound. You want to create a more specific Car class that changes the sound to something unique for cars.
🎯 Goal: You will create a Vehicle class with a makeSound method. Then, you will create a Car class that overrides the makeSound method to show a different sound. Finally, you will print the sounds from both classes.
📋 What You'll Learn
Create a class called Vehicle with a method makeSound that returns the string 'Generic vehicle sound'.
Create a class called Car that extends Vehicle.
Override the makeSound method in Car to return the string 'Vroom!'.
Create instances of both Vehicle and Car.
Print the result of calling makeSound on both instances.
💡 Why This Matters
🌍 Real World
Method overriding is used when you want a child class to change or extend the behavior of a parent class. For example, different types of vehicles make different sounds, so overriding lets each type show its own sound.
💼 Career
Understanding method overriding is important for writing clean, reusable code in object-oriented programming. It is a common skill needed in software development jobs involving JavaScript or other object-oriented languages.
Progress0 / 4 steps
1
Create the Vehicle class
Create a class called Vehicle with a method makeSound that returns the string 'Generic vehicle sound'.
Javascript
Need a hint?

Use class Vehicle {} and inside it define makeSound() that returns the string.

2
Create the Car class that extends Vehicle
Create a class called Car that extends Vehicle.
Javascript
Need a hint?

Use class Car extends Vehicle {} to create the subclass.

3
Override the makeSound method in Car
Inside the Car class, override the makeSound method to return the string 'Vroom!'.
Javascript
Need a hint?

Define makeSound() inside Car that returns 'Vroom!'.

4
Create instances and print sounds
Create an instance called vehicle of the Vehicle class and an instance called car of the Car class. Then, print the result of calling makeSound() on both vehicle and car.
Javascript
Need a hint?

Create instances with new Vehicle() and new Car(). Use console.log() to print the sounds.