Using Abstract Methods in TypeScript
📖 Scenario: Imagine you are creating a simple system for different types of vehicles. Each vehicle must have a method to describe how it moves, but the exact way depends on the vehicle type.
🎯 Goal: You will build an abstract class Vehicle with an abstract method move(). Then, you will create two classes Car and Bicycle that extend Vehicle and provide their own move() method.
📋 What You'll Learn
Create an abstract class called
Vehicle with an abstract method move() that returns a string.Create a class called
Car that extends Vehicle and implements the move() method returning the string 'The car drives on the road.'.Create a class called
Bicycle that extends Vehicle and implements the move() method returning the string 'The bicycle pedals along the path.'.Create instances of
Car and Bicycle and store them in variables myCar and myBike respectively.Print the results of calling
move() on both myCar and myBike.💡 Why This Matters
🌍 Real World
Abstract methods are used in software design to define a common interface for different types of objects, like vehicles, animals, or shapes, ensuring they all have certain behaviors.
💼 Career
Understanding abstract classes and methods is important for designing clean, maintainable code in many programming jobs, especially when working with object-oriented languages like TypeScript.
Progress0 / 4 steps