0
0
Typescriptprogramming~20 mins

Abstract classes in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Abstract Classes in TypeScript
📖 Scenario: You are creating a simple program to represent different types of vehicles. Each vehicle has a method to describe its sound, but the exact sound depends on the type of vehicle.
🎯 Goal: Build a TypeScript program using an abstract class called Vehicle with an abstract method makeSound(). Then create two classes Car and Motorcycle that extend Vehicle and implement the makeSound() method. Finally, create instances and print their sounds.
📋 What You'll Learn
Create an abstract class called Vehicle with an abstract method makeSound() that returns a string.
Create a class Car that extends Vehicle and implements makeSound() to return the string 'Vroom'.
Create a class Motorcycle that extends Vehicle and implements makeSound() to return the string 'Braaap'.
Create one instance of Car called myCar and one instance of Motorcycle called myMotorcycle.
Print the result of calling makeSound() on myCar and myMotorcycle.
💡 Why This Matters
🌍 Real World
Abstract classes help organize code when you have a general concept with shared behavior but different specific details, like different types of vehicles.
💼 Career
Understanding abstract classes is important for designing clean, reusable code in many software development jobs, especially when working with object-oriented programming.
Progress0 / 4 steps
1
Create the abstract class Vehicle
Create an abstract class called Vehicle with an abstract method makeSound() that returns a string.
Typescript
Need a hint?

Use the abstract keyword before the class and the method. The method should have no body.

2
Create Car and Motorcycle classes
Create a class Car that extends Vehicle and implements makeSound() to return 'Vroom'. Also create a class Motorcycle that extends Vehicle and implements makeSound() to return 'Braaap'.
Typescript
Need a hint?

Use extends Vehicle to inherit. Implement makeSound() with the correct return strings.

3
Create instances of Car and Motorcycle
Create one instance of Car called myCar and one instance of Motorcycle called myMotorcycle.
Typescript
Need a hint?

Use new Car() and new Motorcycle() to create instances.

4
Print the sounds of the vehicles
Print the result of calling makeSound() on myCar and myMotorcycle.
Typescript
Need a hint?

Use console.log() to print the sounds.