When is inheritance useful, and when should you prefer composition?
Reveal the answer and explore the explanation.
04MediumCoreWhen is inheritance useful, and when should you prefer composition?
Answer
MediumCore
When is inheritance useful, and when should you prefer composition?
Answer
Inheritance expresses a subtype relationship. Composition delegates work to contained objects and can reduce coupling.
Practical example
Compose a pipeline from a tokenizer, retriever and generator.
Reference: Further reading ↗