Bird
Raised Fist0

Which of the following is a common trade-off or limitation when favoring composition over inheritance in object-oriented design?

medium🪤 Complexity Trap Q13 of Q15
OOP & Design Patterns - Composition vs Inheritance - Favour Composition, Why & When
Which of the following is a common trade-off or limitation when favoring composition over inheritance in object-oriented design?
AComposition always leads to more complex code and harder maintenance than inheritance
BComposition can increase the number of objects and indirection, potentially impacting performance
CComposition prevents code reuse since behaviors cannot be shared
DComposition forces tight coupling between composed objects
Step-by-Step Solution
  1. Step 1: Evaluate each option's validity

    Composition always leads to more complex code and harder maintenance than inheritance is false; composition often improves maintainability. Composition prevents code reuse since behaviors cannot be shared is false; composition promotes code reuse via behavior objects. Composition forces tight coupling between composed objects is false; composition reduces coupling by separating concerns.
  2. Step 2: Understand the trade-off

    Composition introduces more objects and delegation layers, which can add runtime overhead and complexity in object management.
  3. Step 3: Confirm correct trade-off

    Composition can increase the number of objects and indirection, potentially impacting performance correctly identifies the potential performance and complexity cost of increased indirection.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Composition trades off some runtime overhead for flexibility and maintainability.
Quick Trick: Composition trades flexibility for some performance overhead, not complexity or coupling.
Common Mistakes:
MISTAKES
  • Believing composition always simplifies code without cost
  • Thinking composition prevents code reuse
Trap Explanation:
PITFALL
  • Candidates often overlook the runtime cost of delegation and object creation in composition.
Interviewer Note:
CONTEXT
  • Tests awareness of practical trade-offs when choosing composition over inheritance.
Master "Composition vs Inheritance - Favour Composition, Why & When" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes