Bird
Raised Fist0

When using inheritance extensively to share code, what is a common hidden cost compared to composition?

medium🪤 Complexity Trap Q6 of Q15
OOP & Design Patterns - Composition vs Inheritance - Favour Composition, Why & When
When using inheritance extensively to share code, what is a common hidden cost compared to composition?
AReduced flexibility to change behaviors at runtime.
BIncreased runtime memory usage due to duplicated objects.
CMore boilerplate code to delegate behavior calls.
DHigher risk of null pointer exceptions.
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance rigidity

    Inheritance fixes behavior at compile time, limiting runtime flexibility.
  2. Step 2: Compare with composition flexibility

    Composition allows swapping behavior objects dynamically, increasing flexibility.
  3. Step 3: Evaluate other options

    Memory usage is not necessarily higher; delegation boilerplate is more common in composition; null pointer risk is unrelated.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Inheritance limits runtime behavior changes compared to composition [OK]
Quick Trick: Inheritance fixes behavior; composition allows runtime changes [OK]
Common Mistakes:
MISTAKES
  • Assuming inheritance uses more memory than composition
  • Confusing delegation overhead with inheritance
Trap Explanation:
PITFALL
  • Candidates often overlook runtime flexibility differences, focusing on code reuse only.
Interviewer Note:
CONTEXT
  • Tests understanding of behavioral flexibility trade-offs.
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