Object Oriented Programming In Typescript Interview Questions
Comprehensive object oriented programming in typescript interview questions and answers for TypeScript. Prepare for your next job interview with expert guidance.
Questions Overview
1. What are classes in TypeScript and how do they differ from JavaScript classes?
Basic2. Explain access modifiers in TypeScript classes.
Basic3. What are abstract classes in TypeScript?
Moderate4. How does inheritance work in TypeScript?
Basic5. What are parameter properties in TypeScript?
Moderate6. How do you implement interfaces in TypeScript classes?
Basic7. What are static members in TypeScript classes?
Basic8. How do you use getters and setters in TypeScript?
Moderate9. What is method overriding in TypeScript?
Moderate10. How do you implement polymorphism in TypeScript?
Advanced11. What are private constructors and what are they used for?
Advanced12. How do you implement the Singleton pattern in TypeScript?
Advanced13. What is the 'readonly' modifier and how is it used in classes?
Moderate14. How do you handle method overloading in TypeScript?
Advanced15. What are abstract methods and when should you use them?
Moderate16. How do you implement the Factory pattern in TypeScript?
Advanced17. What is the protected constructor pattern?
Advanced18. How do you implement mixins in TypeScript?
Advanced19. What are index signatures in classes?
Moderate20. How do you implement the Observer pattern in TypeScript?
Advanced21. What is the difference between composition and inheritance in TypeScript?
Advanced22. How do you use the 'super' keyword in TypeScript?
Basic23. What are type guards in class hierarchies?
Moderate1. What are classes in TypeScript and how do they differ from JavaScript classes?
BasicTypeScript classes are blueprints for creating objects that extend JavaScript classes with additional features like access modifiers (public, private, protected), parameter properties, abstract classes, and interface implementation. They provide compile-time type checking and better encapsulation. Example: class Person { private name: string; constructor(name: string) { this.name = name; } }
2. Explain access modifiers in TypeScript classes.
BasicTypeScript supports three access modifiers: public (default, accessible everywhere), private (only within the declaring class), and protected (within declaring class and derived classes). These modifiers help enforce encapsulation and control access to class members. Example: class Employee { private salary: number; protected id: string; public name: string; }
3. What are abstract classes in TypeScript?
ModerateAbstract classes are base classes that cannot be instantiated directly and may contain abstract methods that must be implemented by derived classes. They're defined using the 'abstract' keyword. They're useful for defining common behavior while forcing specific implementations in subclasses. Example: abstract class Animal { abstract makeSound(): void; move() { console.log('moving...'); } }
4. How does inheritance work in TypeScript?
BasicTypeScript supports single inheritance using the 'extends' keyword. A class can inherit properties and methods from another class, and can override methods from the parent class. Multiple inheritance is achieved through interfaces. Example: class Employee extends Person { constructor(name: string, private department: string) { super(name); } }
5. What are parameter properties in TypeScript?
ModerateParameter properties are a TypeScript shorthand that allows you to both declare and initialize class members in the constructor parameters. They're created by adding an access modifier to the parameter. Example: class Person { constructor(private name: string, public age: number) {} } This creates and initializes 'name' and 'age' properties automatically.
6. How do you implement interfaces in TypeScript classes?
BasicClasses can implement interfaces using the 'implements' keyword. The class must provide implementations for all properties and methods defined in the interface. Multiple interfaces can be implemented using comma separation. Example: interface Printable { print(): void; } class Document implements Printable { print() { console.log('printing...'); } }
7. What are static members in TypeScript classes?
BasicStatic members (properties and methods) belong to the class itself rather than instances of the class. They're defined using the 'static' keyword and are accessed using the class name. Example: class Calculator { static PI = 3.14159; static add(x: number, y: number): number { return x + y; } }
8. How do you use getters and setters in TypeScript?
ModerateTypeScript supports JavaScript's get and set accessors with additional type safety. They allow you to add logic when accessing or modifying class properties. Example: class Circle { private _radius: number; get radius(): number { return this._radius; } set radius(value: number) { if (value >= 0) this._radius = value; } }
9. What is method overriding in TypeScript?
ModerateMethod overriding allows a subclass to provide a specific implementation of a method that is already defined in its parent class. TypeScript ensures type safety in overridden methods. Example: class Animal { makeSound() { return 'noise'; } } class Dog extends Animal { override makeSound() { return 'woof'; } }
10. How do you implement polymorphism in TypeScript?
AdvancedPolymorphism in TypeScript can be achieved through method overriding and interfaces. It allows objects of different classes to be treated as objects of a common base class or interface. Example: interface Shape { area(): number; } class Circle implements Shape { area() { return Math.PI * r * r; } } class Rectangle implements Shape { area() { return w * h; } }
11. What are private constructors and what are they used for?
AdvancedPrivate constructors prevent class instantiation from outside the class. They're commonly used in singleton pattern implementation or utility classes that shouldn't be instantiated. Example: class Utility { private constructor() {} static helper() { return 'help'; } }
12. How do you implement the Singleton pattern in TypeScript?
AdvancedSingleton pattern ensures a class has only one instance. In TypeScript, it's implemented using a private constructor and static instance. Example: class Singleton { private static instance: Singleton; private constructor() {} static getInstance(): Singleton { if (!Singleton.instance) { Singleton.instance = new Singleton(); } return Singleton.instance; } }
13. What is the 'readonly' modifier and how is it used in classes?
ModerateThe 'readonly' modifier makes class properties immutable after initialization. They must be initialized at declaration or in the constructor. Example: class Config { readonly apiKey: string; constructor(key: string) { this.apiKey = key; } } Once set, readonly properties cannot be changed.
14. How do you handle method overloading in TypeScript?
AdvancedTypeScript supports method overloading through function declarations with different parameter types or numbers. Implementation is done with a single function that handles all overloads. Example: class Calculator { add(x: number, y: number): number; add(x: string, y: string): string; add(x: any, y: any): any { return x + y; } }
15. What are abstract methods and when should you use them?
ModerateAbstract methods are methods declared in abstract classes without implementation. They must be implemented by derived classes. They're used when a base class knows a method is needed but specific implementation depends on the derived class. Example: abstract class Shape { abstract calculateArea(): number; }
16. How do you implement the Factory pattern in TypeScript?
AdvancedThe Factory pattern provides an interface for creating objects without specifying exact classes. In TypeScript, it's implemented using abstract classes or interfaces with factory methods. Example: abstract class Creator { abstract createProduct(): Product; } class ConcreteCreator extends Creator { createProduct(): Product { return new ConcreteProduct(); } }
17. What is the protected constructor pattern?
AdvancedProtected constructors allow instantiation only from within the class itself or its derived classes. This pattern is useful for creating base classes that shouldn't be instantiated directly but can be extended. Example: class Base { protected constructor() {} } class Derived extends Base { constructor() { super(); } }
18. How do you implement mixins in TypeScript?
AdvancedMixins are a way to combine multiple classes into one. TypeScript implements mixins using class expressions and a helper function. Example: type Constructor<T = {}> = new (...args: any[]) => T; function Timestamped<TBase extends Constructor>(Base: TBase) { return class extends Base { timestamp = Date.now(); }; }
19. What are index signatures in classes?
ModerateIndex signatures in classes allow you to define dynamic properties with a specific type. They're defined using [key: string]: type syntax. Example: class Dictionary { [key: string]: string; constructor() {} add(key: string, value: string) { this[key] = value; } }
20. How do you implement the Observer pattern in TypeScript?
AdvancedThe Observer pattern is implemented using interfaces for Subject and Observer, with the Subject maintaining a list of observers and notifying them of changes. Example: interface Observer { update(data: any): void; } class Subject { private observers: Observer[] = []; addObserver(observer: Observer) { this.observers.push(observer); } notify(data: any) { this.observers.forEach(observer => observer.update(data)); } }
21. What is the difference between composition and inheritance in TypeScript?
AdvancedComposition involves creating complex objects by combining simpler ones, while inheritance creates relationships between classes. Composition is often preferred as it provides more flexibility and looser coupling. Example of composition: class Car { private engine: Engine; private wheels: Wheel[]; constructor() { this.engine = new Engine(); this.wheels = [new Wheel(), new Wheel(), new Wheel(), new Wheel()]; } }
22. How do you use the 'super' keyword in TypeScript?
BasicThe 'super' keyword is used to call methods or access properties on the parent class. It's commonly used in constructors of derived classes and when overriding methods. Example: class Child extends Parent { constructor(name: string) { super(name); } override getName(): string { return 'Child: ' + super.getName(); } }
23. What are type guards in class hierarchies?
ModerateType guards in class hierarchies help narrow down types when working with inheritance. The instanceof operator is commonly used as a type guard. Example: class Animal {} class Dog extends Animal { bark() {} } function makeNoise(animal: Animal) { if (animal instanceof Dog) { animal.bark(); } }