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
classis a blueprint for creating objects.constructor()initializes object properties.thisrefers to the current object.student1is an instance (object) of theStudentclass.
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
privaterestricts 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
extendsenables 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
- What is OOP?
- What is a class and an object?
- What is a constructor?
- What is the purpose of
thiskeyword? - Difference between interface and class?
- What is encapsulation?
- What are access modifiers in TypeScript?
- Difference between
public,private, andprotected? - What is inheritance?
- What is polymorphism?
Intermediate Level
- Difference between abstract class and interface?
- Can a class implement multiple interfaces?
- Can a class extend multiple classes?
- What is method overriding?
- What are static members?
- How do getters and setters work?
- What is constructor overloading?
- What is dependency injection?
- What is composition in OOP?
- What is aggregation?
Advanced Level
- Design a Library Management System using OOP.
- Design an Employee Payroll System.
- Implement a Shopping Cart using OOP.
- Create a Playwright Page Object Model using OOP principles.
- Design a Banking Application using abstraction and inheritance.
- Build a Vehicle Rental System using interfaces.
- Create a Logger Utility using Singleton Pattern.
- Implement Factory Design Pattern in TypeScript.
- Implement Strategy Design Pattern in TypeScript.
- 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