0
0
Typescriptprogramming~30 mins

Abstract methods in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the abstract class Vehicle with an abstract method move
Create an abstract class called Vehicle with an abstract method move() 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 classes Car and Bicycle that extend Vehicle and implement move
Create a class called Car that extends Vehicle and implements the move() method returning the string 'The car drives on the road.'. Also create a class called Bicycle that extends Vehicle and implements the move() method returning the string 'The bicycle pedals along the path.'.
Typescript
Need a hint?

Remember to use extends Vehicle and implement the move() method with the exact return strings.

3
Create instances of Car and Bicycle
Create an instance of Car called myCar and an instance of Bicycle called myBike.
Typescript
Need a hint?

Use const to create the instances with the exact variable names myCar and myBike.

4
Print the results of calling move() on myCar and myBike
Write two console.log statements to print the results of calling move() on myCar and myBike.
Typescript
Need a hint?

Use console.log(myCar.move()) and console.log(myBike.move()) to print the messages.