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