Reading OOP Patterns
Inheritance, polymorphism, composition, interfaces, and the Strategy pattern in plain English
OOP pattern vocabulary
- Inheritance — "is-a"; extends parent class; overrides methods
- Composition — "has-a"; holds a reference and delegates; preferred over deep inheritance
- Polymorphism — same call dispatches to correct subclass method at runtime
- Interface — contract (what methods must exist); implementations are interchangeable
- Strategy pattern — extract varying behavior as a swappable dependency
Question 0 of 5
A class diagram shows: Dog extends Animal. Which OOP concept does this represent, and what does it imply?
Inheritance — "is-a" relationship; Dog inherits Animal's attributes and methods. Inheritance vocabulary:
- extends / inherits from / is a subclass of — Dog is a subclass; Animal is the superclass/parent class
- override — Dog provides its own implementation of a method from Animal
- super() — calls the parent class's method
- "is-a" relationship — Dog IS an Animal; use inheritance when this holds
- "has-a" relationship — use composition instead (Dog HAS an Engine = wrong; Dog HAS a Collar = composition)
What does "polymorphism" mean in the context of this code?function makeSound(animal: Animal) {
animal.speak();
}
where Dog and Cat both extend Animal and override speak().
Polymorphism: same call dispatches to the right implementation based on the actual type. Polymorphism vocabulary:
- Polymorphism — "many forms"; a single interface (
animal.speak()) behaves differently for each concrete type - Runtime dispatch — the method called is determined at runtime based on the actual object type
- Liskov Substitution Principle (LSP) — any subclass should be usable wherever the parent class is expected, without breaking correctness
Bird class that overrides speak() doesn't require changing makeSound().A design doc says: "We prefer composition over inheritance here." What does this mean?
Include an instance of the class as a field and delegate to it instead of inheriting. Composition vs inheritance vocabulary:
- Inheritance:
class EmailService extends SmtpClient— EmailService IS an SmtpClient (wrong if you only need send()) - Composition:
class EmailService { smtp = new SmtpClient() }— EmailService HAS an SmtpClient; delegatesthis.smtp.send()
- Inheritance creates tight coupling to the parent class
- Composition allows swapping the inner component (e.g., for testing)
- "Favor composition over inheritance" is a GoF design principle
What does this design pattern do?interface Logger { log(msg: string): void; }
class ConsoleLogger implements Logger { ... }
class FileLogger implements Logger { ... }
Interface defines a contract; implementations are interchangeable. Interface pattern vocabulary:
- Interface / Protocol / Abstract class — defines what methods must exist, not how they work
- implements / conforms to — the class provides the required methods
- Program to an interface — depend on Logger, not ConsoleLogger; allows swapping implementations
- Dependency inversion principle — high-level code depends on abstractions, not concrete classes
Logger via constructor; in production use FileLogger; in tests use a mock that records calls. Review phrase: "Depend on the Logger interface, not FileLogger directly — this lets us swap implementations without changing callers."A PR description says: "Refactored to use the Strategy pattern." What does this mean in plain English?
Extract the varying behavior into a separate class/function; pass it in as a dependency. Strategy pattern vocabulary:
- Strategy — an interchangeable algorithm or behavior:
SortStrategy,PricingStrategy,AuthStrategy - Context — the class that uses the strategy; delegates the varying behavior to the strategy object
- "Open for extension, closed for modification" — add new strategies without changing the context class
Sorter(strategy: SortStrategy) with QuickSortStrategy, MergeSortStrategy, TimSortStrategy as interchangeable implementations. Code review: "The sorting algorithm is hardcoded. Extract it as a strategy so callers can choose the algorithm without modifying Sorter."