Bird
Raised Fist0

What is the flaw in this reasoning?

medium🐞 Bug Identification Q7 of Q15
OOP & Design Patterns - Composition vs Inheritance - Favour Composition, Why & When
A developer writes a class hierarchy where subclasses override methods to change behavior. They claim this is always better than composition because it avoids object creation overhead. What is the flaw in this reasoning?
AInheritance can cause tight coupling and fragile base class problems.
BComposition always uses less memory than inheritance.
COverriding methods is slower than delegation in composition.
DObject creation overhead is negligible compared to method calls.
Step-by-Step Solution
Solution:
  1. Step 1: Identify coupling issues with inheritance

    Inheritance tightly couples subclasses to base class implementation, causing fragility.
  2. Step 2: Evaluate object creation overhead claim

    Object creation overhead is usually minimal and outweighed by flexibility benefits.
  3. Step 3: Analyze other options

    Composition does not always use less memory; overriding is generally faster than delegation; overhead is context-dependent.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Inheritance causes tight coupling and fragility, not just performance issues [OK]
Quick Trick: Inheritance causes tight coupling, not just performance overhead [OK]
Common Mistakes:
MISTAKES
  • Equating performance overhead with design quality
  • Assuming composition always uses less memory
Trap Explanation:
PITFALL
  • Candidates focus on performance and ignore coupling and maintainability issues.
Interviewer Note:
CONTEXT
  • Checks understanding of design quality beyond performance.
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