w3resource

TypeScript Classes and Object-Oriented Programming (OOP) Practice Exercises

TypeScript Classes and OOP [24 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

These exercises cover the class definitions, constructors, inheritance, abstract classes, access modifiers and static members in TypeScript. Each exercise will help you build your TypeScript skills and understand different aspects of object-oriented programming in TypeScript.

Class Definitions:

1. Write a TypeScript class called Bus with the properties make, model, and year. Implement a constructor that initializes these properties when a Bus object is created.
Click me to see the solution

2. Write a TypeScript class called Bus with the properties make, model, and year. Add a method start() that prints a message indicating that the Bus is starting.  
Click me to see the solution

3. Write a TypeScript class called SUV (Sports Utility Vehicle) that extends the Car class. Add a property to represent whether the SUV is suitable for off-road driving. Implement a method that toggles off-road capability and prints a message accordingly.  
Click me to see the solution

4. Write a TypeScript class that defines a base class Car with properties like make, model, and year, along with a start() method that prints a starting message. Now create a class called Engine with the properties horsepower and fuelType. Modify the Car class to include an instance of the Engine class as a property. Implement a method printCarDetails() in the Car class that prints both car and engine details. 
Click me to see the solution

Constructors:

5. Write a TypeScript program that creates a class called Student with properties name and class. Implement a constructor that initializes these properties when a Student object is created.  
Click me to see the solution

6. Write a TypeScript program that creates a class called Student with properties name and roll number. Add constructor overloading to support multiple ways of initializing a Student object. Implement one constructor that takes both name and roll number as parameters and another constructor that takes only name, assuming the roll number is unknown.  
Click me to see the solution

7. Write a TypeScript class called Shape with properties like color. Implement a constructor that initializes the color property when a Shape object is created. Then, create a derived class Circle that extends Shape. Implement a constructor for the Circle class that takes color and radius as parameters and initializes them along with the color property of the base class.  
Click me to see the solution

8. Write a TypeScript class called Student with properties name and age. Implement a constructor that initializes these properties when a Student object is created. Additionally, add validation to ensure that the age provided is a positive number. If the age is not positive, set it to a default value of 0.  
Click me to see the solution

Inheritance:

9. Write a TypeScript class called Animal with properties name and species. Implement a constructor that initializes these properties when an Animal object is created. Next, create a derived class Tiger that extends Animal. Add a method roar() to the Tiger class that prints a message indicating that the tiger is roaring.  
Click me to see the solution

10. Write a TypeScript class called Person with properties name and age. Implement a constructor that initializes these properties when a Person object is created. Then, create a derived class Employee that extends Person. Override the constructor of the Employee class to include an additional property employeeId.  
Click me to see the solution

11. Write a TypeScript program that creates a class called Shape with properties color and a method draw(). This program prints a message indicating that the shape is being drawn. Then, create a derived class Circle that extends Shape. Override the draw() method in the Circle class to provide a specific implementation for drawing a circle.  
Click me to see the solution

12. Write a TypeScript program that defines a class called Vehicle with properties make and model. Implement a constructor that initializes these properties when a Vehicle object is created. Then, create a derived class Car that extends Vehicle. Finally, create a derived class SportsCar that extends Car. Ensure that properties are inherited correctly and each class has its own constructor.  
Click me to see the solution

Abstract Classes:

13. Write a TypeScript program that creates an abstract class called Shape with properties like color and an abstract method getPerimeter(). Implement derived classes for specific shapes (e.g., Circle, Rectangle) that extend Shape and provide concrete implementations for calculating their perimeters.  
Click me to see the solution

14. Write a TypeScript exercise that defines an abstract class called Animal with properties like name and an abstract method makeSound(). Create derived classes (e.g., Tiger, Lion) that extend Animal and implement the makeSound() method with the unique sound each animal makes.  
Click me to see the solution

15. Write a TypeScript program that defines an abstract class Employee with properties such as name, employeeId, and an abstract method calculateSalary(). Create derived classes (e.g., FullTimeEmployee, PartTimeEmployee) that extend Employee and provide salary calculation logic based on employment type.  
Click me to see the solution

Geometric Shapes:

16. Write a TypeScript program that creates an abstract class GeometricShape with properties like name and abstract methods for calculating area and perimeter. Implement derived classes for specific shapes (e.g., Circle, Rectangle, Triangle) that extend GeometricShape and provide concrete implementations for area and perimeter calculations.  
Click me to see the solution

Access Modifiers

17. Write a TypeScript class called Student with the following properties and methods:

  • private name: string
  • protected studentId: number
The class should have a constructor that accepts a name and student ID. Implement a method:
public displayInfo(): void that displays the student's name and ID.
Make sure that the studentId property is accessible only within the class and its subclasses.
Click me to see the solution

18. Write a TypeScript class called BankAccount with the following properties and methods:

  • private accountNumber: string
  • protected balance: number
The class should have a constructor that accepts an account number and initializes the balance to 0. It should also include methods:
public deposit(amount: number): void to add funds to the account.
public withdraw(amount: number): void to deduct funds from the account.
Only the class and its subclasses should have access to the balance property.
Click me to see the solution

19. Write a TypeScript program that creates a class called Car with the following properties and methods:

  • protected make: string
  • protected model: string
The class should have a constructor that accepts make and model. Implement a method:
public getCarInfo(): string that returns a string containing the make and model of the car.
Make sure that the make and model properties are only accessible within the class and its subclasses.
Click me to see the solution

20. Write a TypeScript program that creates a class called Animal with the following properties and methods:

  • protected name: string
  • private age: number
The class should have a constructor that accepts name and age. Implement a method:
public introduce(): string that returns a string introducing the animal, including its name and age.
Ensure that the age property is accessible only within the class.
Click me to see the solution

21. Write a TypeScript class called Counter with a static property count initialized to 0. Implement a static method increment() that increments the count by 1. Implement another static method getCount() that returns the current count value. Test the class by calling both methods
Click me to see the solution

22. Write a TypeScript program that creates a class with static methods for common mathematical operations, such as add, subtract, multiply, and divide. Each method should accept two numbers as parameters and return the operation result. Test the class by performing various mathematical operations using its static methods.
Click me to see the solution

23. Write a TypeScript class called Singleton that follows the Singleton design pattern. The class should have a private static instance of itself and a static method getInstance() that returns the instance. Ensure that only one instance of the class can be created. Test the Singleton pattern by creating multiple instances and checking if they are the same instance.
Click me to see the solution

24. Write a TypeScript utility class with static methods for common utility functions, such as formatDate, generateRandomString, and capitalizeString. Each method should provide the respective utility functionality and be accessible without creating an instance of the class. Test the utility class by using its static methods to perform various tasks.
Click me to see the solution

More to Come !

TypeScript Editor

See the Pen TypeScript by w3resource (@w3resource) on CodePen.


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.