TypeScript OOPs Practice Questions with Explanations

Object-Oriented Programming (OOP) is one of the most important concepts in TypeScript. Understanding OOP helps you build scalable, maintainable, and reusable applications.


1. Classes and Objects

Question 1

Create a Student class with properties name and age. Create an object and display its details.

Solution

class Student {
name: string;
age: number;

constructor(name: string, age: number) {
this.name = name;
this.age = age;
}

display(): void {
console.log(`Name: ${this.name}, Age: ${this.age}`);
}
}

const student1 = new Student("John", 20);
student1.display();

Explanation

  • class is a blueprint for creating objects.
  • constructor() initializes object properties.
  • this refers to the current object.
  • student1 is an instance (object) of the Student class.

2. Constructor Practice

Question 2

Create an Employee class with id, name, and salary. Initialize values through a constructor.

Solution

class Employee {
constructor(
public id: number,
public name: string,
public salary: number
) {}

getDetails(): void {
console.log(this.id, this.name, this.salary);
}
}

const emp = new Employee(101, "David", 50000);
emp.getDetails();

Explanation

TypeScript allows access modifiers directly in constructor parameters, reducing code duplication.


3. Encapsulation

Question 3

Create a BankAccount class where the balance cannot be accessed directly.

Solution

class BankAccount {
private balance: number = 0;

deposit(amount: number): void {
this.balance += amount;
}

getBalance(): number {
return this.balance;
}
}

const account = new BankAccount();
account.deposit(5000);

console.log(account.getBalance());

Explanation

  • private restricts direct access to class members.
  • Data is protected through methods.
  • This concept is called Encapsulation.

4. Inheritance

Question 4

Create a Vehicle class and a Car class that inherits from it.

Solution

class Vehicle {
start(): void {
console.log("Vehicle Started");
}
}

class Car extends Vehicle {
drive(): void {
console.log("Car is Driving");
}
}

const car = new Car();

car.start();
car.drive();

Explanation

  • extends enables inheritance.
  • Child class gets access to parent class methods.
  • Promotes code reusability.

5. Method Overriding

Question 5

Override a method from the parent class.

Solution

class Animal {
makeSound(): void {
console.log("Animal Sound");
}
}

class Dog extends Animal {
makeSound(): void {
console.log("Bark");
}
}

const dog = new Dog();
dog.makeSound();

Explanation

The child class provides its own implementation of the parent’s method.

Output:

Bark

6. Polymorphism

Question 6

Demonstrate runtime polymorphism.

Solution

class Shape {
draw(): void {
console.log("Drawing Shape");
}
}

class Circle extends Shape {
draw(): void {
console.log("Drawing Circle");
}
}

class Rectangle extends Shape {
draw(): void {
console.log("Drawing Rectangle");
}
}

const shapes: Shape[] = [
new Circle(),
new Rectangle()
];

shapes.forEach(shape => shape.draw());

Explanation

The same method (draw) behaves differently depending on the object type.


7. Abstraction

Question 7

Create an abstract class and implement it.

Solution

abstract class Payment {
abstract pay(amount: number): void;
}

class CreditCardPayment extends Payment {
pay(amount: number): void {
console.log(`Paid ${amount} using Credit Card`);
}
}

const payment = new CreditCardPayment();
payment.pay(1000);

Explanation

  • Abstract classes cannot be instantiated.
  • Abstract methods must be implemented by child classes.

8. Interface

Question 8

Create an interface for login functionality.

Solution

interface Login {
login(username: string, password: string): void;
}

class User implements Login {
login(username: string, password: string): void {
console.log(`${username} logged in`);
}
}

const user = new User();
user.login("admin", "1234");

Explanation

Interfaces define a contract that classes must follow.


9. Getter and Setter

Question 9

Use getters and setters to manage a private property.

Solution

class Product {
private _price: number = 0;

get price(): number {
return this._price;
}

set price(value: number) {
if (value > 0) {
this._price = value;
}
}
}

const p = new Product();

p.price = 1000;

console.log(p.price);

Explanation

Getters and setters provide controlled access to properties.


10. Static Members

Question 10

Create a static method and property.

Solution

class MathUtility {
static PI: number = 3.14;

static calculateArea(radius: number): number {
return MathUtility.PI * radius * radius;
}
}

console.log(MathUtility.calculateArea(5));

Explanation

Static members belong to the class itself, not its objects.


Interview-Oriented Practice Questions

Beginner Level

  1. What is OOP?
  2. What is a class and an object?
  3. What is a constructor?
  4. What is the purpose of this keyword?
  5. Difference between interface and class?
  6. What is encapsulation?
  7. What are access modifiers in TypeScript?
  8. Difference between public, private, and protected?
  9. What is inheritance?
  10. What is polymorphism?

Intermediate Level

  1. Difference between abstract class and interface?
  2. Can a class implement multiple interfaces?
  3. Can a class extend multiple classes?
  4. What is method overriding?
  5. What are static members?
  6. How do getters and setters work?
  7. What is constructor overloading?
  8. What is dependency injection?
  9. What is composition in OOP?
  10. What is aggregation?

Advanced Level

  1. Design a Library Management System using OOP.
  2. Design an Employee Payroll System.
  3. Implement a Shopping Cart using OOP.
  4. Create a Playwright Page Object Model using OOP principles.
  5. Design a Banking Application using abstraction and inheritance.
  6. Build a Vehicle Rental System using interfaces.
  7. Create a Logger Utility using Singleton Pattern.
  8. Implement Factory Design Pattern in TypeScript.
  9. Implement Strategy Design Pattern in TypeScript.
  10. Create a Test Automation Framework structure using OOP concepts.

Real-Time Automation Framework Question

Question

Design a Playwright Page Object Model using OOP concepts.

class LoginPage {
constructor(private page: Page) {}

async login(username: string, password: string) {
await this.page.fill("#username", username);
await this.page.fill("#password", password);
await this.page.click("#loginBtn");
}
}

OOP Concepts Used

  • Class → LoginPage
  • Object → Page Object instance
  • Encapsulation → Locators and methods inside class
  • Abstraction → Test scripts don’t know implementation details
  • Reusability → Common methods reused across tests

Leave a Comment