QUESTION 4 OF 10

When is inheritance useful, and when should you prefer composition?

Reveal the answer and explore the explanation.

04
MediumCore

When is inheritance useful, and when should you prefer composition?

Answer
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 ↗

Last 30 min revisionHigh-yield interview Q&As