Replace Inheritance with Delegation
Domain: Refactoring
π― Motivation
Inheritance is for βis-aβ relationships, but often misused for code reuse. When a subclass only wants some parent behavior, delegation (holding a reference) is more flexible and safer.
π Concrete Example
A Stack should not inherit Vector just to reuse storage. Instead, Stack holds a list internally and delegates push and pop to it, exposing only stack-like methods.
π Analogy
You do not become a professional chef to use their kitchen. You rent the kitchen and hire the chef for specific tasks β you get what you need without the unwanted obligations.