Classes, Inheritance & OOP Flashcards

Classes & Constructors Access Modifiers: public, private, protected, readonly Inheritance Implements vs Extends Abstract Classes (39 cards)

1
Q

What is a class in TypeScript?

A

A class is a blueprint for creating objects with shared properties and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a constructor in TypeScript?

A

A constructor is a special method in a class that gets called when an instance is created, used to initialize class properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are access modifiers in TypeScript?

A

Access modifiers control visibility of class members: public (default), private, protected, and readonly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does ‘public’ mean in TypeScript?

A

A public member can be accessed anywhere, both inside and outside the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does ‘private’ mean in TypeScript?

A

A private member is accessible only within the class where it’s declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does ‘protected’ mean in TypeScript?

A

A protected member is accessible within the class and its subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does ‘readonly’ mean in TypeScript?

A

A readonly property can be assigned only during initialization or in the constructor, and cannot be changed afterward.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is inheritance in TypeScript?

A

Inheritance allows a class to acquire properties and methods from another class using the ‘extends’ keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between ‘implements’ and ‘extends’?

A

‘extends’ is used for class inheritance, while ‘implements’ is used to enforce a class to follow an interface contract.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an abstract class in TypeScript?

A

An abstract class can’t be instantiated and is meant to be extended by other classes. It may contain abstract methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of abstract methods?

A

Abstract methods define method signatures without implementation and must be implemented in derived classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a use case for using classes in TypeScript?

A

Encapsulating business logic, modeling entities like User or Order in applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the advantages of using classes in TypeScript?

A

Encapsulation, reusability, code organization, and strong typing support.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a disadvantage of classes in TypeScript?

A

They may lead to overengineering or tightly coupled code if used excessively or improperly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a best practice when using constructors?

A

Keep them simple, only use them for initializing essential properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a best practice for using access modifiers?

A

Use ‘private’ or ‘protected’ to encapsulate internal logic and avoid exposing implementation details.

17
Q

What is a good use case for abstract classes?

A

When you want to define a common base class with default behavior and force subclasses to implement specific methods.

18
Q

How does inheritance impact system design?

A

It promotes code reuse but can lead to rigid hierarchies if not designed carefully. Composition is often preferred.

19
Q

What is the architectural implication of ‘implements’?

A

Encourages loose coupling and adherence to interfaces, enabling better testing and modularity.

20
Q

How does class inheritance affect performance?

A

Minor runtime overhead from prototype chain lookups, but not significant in most applications.

21
Q

How do you debug classes in TypeScript?

A

Use IDE breakpoints, logging in constructors/methods, and ensure source maps are configured for TS files.

22
Q

What is a real-world tradeoff between inheritance and composition?

A

Inheritance creates tight coupling and is less flexible, while composition offers better separation of concerns but may need more boilerplate.

23
Q

What is a common interview question on access modifiers?

A

Explain the difference between public, private, protected, and readonly in TypeScript.

24
Q

What is a gotcha when using private members in TypeScript?

A

Private members declared in TypeScript are only enforced at compile-time, not at runtime.

25
What is a gotcha with abstract classes?
Forgetting to implement all abstract methods in a subclass will cause a compilation error.
26
What is the impact of 'readonly' on maintainability?
It improves immutability and prevents accidental changes to critical data.
27
What is an example of class inheritance?
class Dog extends Animal { bark() {} }
28
What is an example of implementing an interface?
class Car implements Drivable { drive() { ... } }
29
What happens if a class does not implement all interface methods?
TypeScript will throw a compile-time error indicating unimplemented members.
30
What are the implications of using 'extends' excessively?
Can lead to deep, fragile inheritance trees that are hard to maintain and test.
31
When should you prefer composition over inheritance?
When you need flexibility, dynamic behavior, or when classes have different concerns.
32
What happens if you try to instantiate an abstract class?
TypeScript will show a compile-time error: abstract classes cannot be instantiated.
33
How does object-oriented design help in large codebases?
It promotes modular, reusable, and maintainable code structures.
34
What is a typical architectural pattern using classes in Node.js?
Service Layer pattern where services encapsulate business logic using classes.
35
What does 'super' refer to in a subclass constructor?
It calls the constructor of the parent class and must be called before using 'this'.
36
What happens if you forget to call 'super' in a subclass constructor?
TypeScript will throw a compilation error: 'super' must be called before accessing 'this'.
37
What’s a potential issue with deep inheritance chains?
They make debugging and understanding code harder and introduce tight coupling.
38
Why is interface-based design preferred over class inheritance?
It allows for better decoupling, testing, and flexibility in extending functionality.
39
How can readonly and private improve system safety?
They reduce the chances of accidental data mutations and enforce encapsulation.