Mastering OOP Concepts in TypeScript: Essential Interview Questions
In TypeScript interviews, understanding object-oriented programming (OOP) is essential. Below are
some of the most frequently asked OOP questions in TypeScript interviews, with examples to demonstrate how to apply these concepts effectively.
1. What are the Four Pillars of OOP?
The four main principles of OOP are Encapsulation, Abstraction, Inheritance, and Polymorphism. Here’s a TypeScript example for each:
Encapsulation: Restricting access to certain properties by marking them
private
orprotected
.class User { private password: string; constructor(password: string) { this.password = password; } getPassword(): string { return this.password; } } const user = new User("securePassword"); console.log(user.getPassword()); // "securePassword"
Abstraction: Hiding complex implementation details and exposing only the essentials.
abstract class Animal { abstract makeSound(): void; move(): void { console.log("Moving..."); } } class Dog extends Animal { makeSound(): void { console.log("Bark"); } }
Inheritance: Deriving new classes from existing ones, sharing behavior across classes.
class Animal { move() { console.log("Moving..."); } } class Bird extends Animal { fly() { console.log("Flying..."); } }
Polymorphism: Allowing one interface to be used with different types.
class Animal { makeSound(): void { console.log("Some generic sound"); } } class Dog extends Animal { makeSound(): void { console.log("Bark"); } } const myAnimal: Animal = new Dog(); myAnimal.makeSound(); // "Bark"
2. What is Method Overloading?
In TypeScript, method overloading allows a class to have multiple methods with the same name but different parameter types.
class Calculator {
add(a: number, b: number): number;
add(a: string, b: string): string;
add(a: any, b: any): any {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 10)); // 15
console.log(calc.add("Hello, ", "World!")); // "Hello, World!"
3. What is an Interface, and How is it Used?
Interfaces define contracts that classes or objects must follow. They’re crucial in TypeScript to enforce structure.
interface Vehicle {
speed: number;
accelerate(): void;
}
class Car implements Vehicle {
speed: number = 0;
accelerate() {
this.speed += 10;
}
}
4. Explain Dependency Injection
Dependency Injection (DI) is a design pattern where an object receives other objects it depends on. TypeScript with frameworks like NestJS promotes DI for cleaner, modular code.
class Engine {
start() {
console.log("Engine started");
}
}
class Car {
constructor(private engine: Engine) {}
drive() {
this.engine.start();
console.log("Car is driving");
}
}
const engine = new Engine();
const car = new Car(engine);
car.drive(); // Engine started; Car is driving
5. What is the Difference Between Abstract Classes and Interfaces?
While both enforce structure, abstract classes can have method implementations, whereas interfaces can’t. Abstract classes allow shared logic, making them more flexible.
abstract class Shape {
abstract getArea(): number;
printArea() {
console.log(`Area: ${this.getArea()}`);
}
}
class Circle extends Shape {
constructor(private radius: number) {
super();
}
getArea(): number {
return Math.PI * this.radius * this.radius;
}
}
Mastering these OOP concepts will give you a solid foundation for TypeScript interviews and real-world applications.