Classes, Inheritance & OOP Flashcards
Classes & Constructors Access Modifiers: public, private, protected, readonly Inheritance Implements vs Extends Abstract Classes (39 cards)
What is a class in TypeScript?
A class is a blueprint for creating objects with shared properties and methods.
What is a constructor in TypeScript?
A constructor is a special method in a class that gets called when an instance is created, used to initialize class properties.
What are access modifiers in TypeScript?
Access modifiers control visibility of class members: public (default), private, protected, and readonly.
What does ‘public’ mean in TypeScript?
A public member can be accessed anywhere, both inside and outside the class.
What does ‘private’ mean in TypeScript?
A private member is accessible only within the class where it’s declared.
What does ‘protected’ mean in TypeScript?
A protected member is accessible within the class and its subclasses.
What does ‘readonly’ mean in TypeScript?
A readonly property can be assigned only during initialization or in the constructor, and cannot be changed afterward.
What is inheritance in TypeScript?
Inheritance allows a class to acquire properties and methods from another class using the ‘extends’ keyword.
What is the difference between ‘implements’ and ‘extends’?
‘extends’ is used for class inheritance, while ‘implements’ is used to enforce a class to follow an interface contract.
What is an abstract class in TypeScript?
An abstract class can’t be instantiated and is meant to be extended by other classes. It may contain abstract methods.
What is the purpose of abstract methods?
Abstract methods define method signatures without implementation and must be implemented in derived classes.
What is a use case for using classes in TypeScript?
Encapsulating business logic, modeling entities like User or Order in applications.
What are the advantages of using classes in TypeScript?
Encapsulation, reusability, code organization, and strong typing support.
What is a disadvantage of classes in TypeScript?
They may lead to overengineering or tightly coupled code if used excessively or improperly.
What is a best practice when using constructors?
Keep them simple, only use them for initializing essential properties.
What is a best practice for using access modifiers?
Use ‘private’ or ‘protected’ to encapsulate internal logic and avoid exposing implementation details.
What is a good use case for abstract classes?
When you want to define a common base class with default behavior and force subclasses to implement specific methods.
How does inheritance impact system design?
It promotes code reuse but can lead to rigid hierarchies if not designed carefully. Composition is often preferred.
What is the architectural implication of ‘implements’?
Encourages loose coupling and adherence to interfaces, enabling better testing and modularity.
How does class inheritance affect performance?
Minor runtime overhead from prototype chain lookups, but not significant in most applications.
How do you debug classes in TypeScript?
Use IDE breakpoints, logging in constructors/methods, and ensure source maps are configured for TS files.
What is a real-world tradeoff between inheritance and composition?
Inheritance creates tight coupling and is less flexible, while composition offers better separation of concerns but may need more boilerplate.
What is a common interview question on access modifiers?
Explain the difference between public, private, protected, and readonly in TypeScript.
What is a gotcha when using private members in TypeScript?
Private members declared in TypeScript are only enforced at compile-time, not at runtime.