Composition over inheritance
Композиция вместо наследования
When an object needs a capability, prefer giving it another object to do that job over creating a subclass to inherit the behavior. This is a design guideline, not a ban on inheritance.
Also known as: композиция
What the guideline means
Suppose a report exporter needs to write to a file or an in-memory buffer. You could create a subclass for each destination, but the exporter doesn't need to be a kind of file writer. It just needs a writer it can call. Building an object out of collaborators this way is object composition; handing a task to one of those collaborators is delegation.
The useful question is not just “Does this sound like a has-a or an is-a relationship?” It's “What should be able to change without changing the rest of this class?” If clients only need the exporter to produce a report, the destination can be a separate concern rather than part of the exporter's type.
How it works
Give the collaborator a small contract that describes what the caller needs. A report exporter, for instance, can accept any object with a write method that accepts text. In a language such as Java, you might express that contract with an interface. In Python, the caller can simply use the method without requiring a shared base class.
from io import StringIO
class ReportExporter:
def __init__(self, destination):
self._destination = destination
def export(self, count):
self._destination.write(f"Orders: {count}\n")
buffer = StringIO()
ReportExporter(buffer).export(3)
assert buffer.getvalue() == "Orders: 3\n"
The exporter decides what to write; the destination decides where it goes. Production code can pass an open text file, while a test can pass StringIO and inspect the output without touching the filesystem. Passing the destination in is dependency injection. Composition doesn't require injection: an object can also create its own collaborator and delegate to it.
Where it helps and what it costs
Composition pays off when behaviors vary independently. If the exporter later needs both CSV and plain-text output, a formatter and a destination can be chosen separately instead of defining a subclass for every format–destination pair. This is the idea behind the Strategy pattern: put a varying behavior behind a contract and delegate to the chosen implementation.
Each collaborator adds a boundary to understand and sometimes more wiring or forwarding code. Don't introduce an interface for every helper just in case. Inheritance remains reasonable when a subclass can stand in for its base type without breaking the promises callers rely on, or when subclassing is a framework's documented extension mechanism. Reusing a few methods alone is a weak reason to inherit an entire base class.
In an interview
You may be asked to refactor a hierarchy that has grown a subclass for each combination of options. Identify which behaviors change independently, sketch their contracts, and show where the collaborators are selected. If you keep a subclass, explain which base-type guarantees it preserves. That makes the trade-off concrete instead of turning the guideline into a rule against inheritance.