Bird
Raised Fist0

When designing the Library Management System, what is a key trade-off when using inheritance to model different types of library items (e.g., books, magazines, DVDs) versus using composition with a common Item interface?

medium🪤 Complexity Trap Q13 of Q15
OOP & Design Patterns - Design a Library Management System - LLD with Relationships & Edge Cases
When designing the Library Management System, what is a key trade-off when using inheritance to model different types of library items (e.g., books, magazines, DVDs) versus using composition with a common Item interface?
AComposition leads to code duplication but improves runtime performance due to fewer indirections
BInheritance simplifies code reuse but reduces flexibility to add new item types without modifying base classes
CInheritance allows dynamic behavior changes at runtime, while composition fixes behavior at compile time
DComposition forces all item types to share the same data fields, limiting customization
Step-by-Step Solution
  1. Step 1: Understand inheritance trade-offs

    Inheritance promotes reuse but creates tight coupling and fragile base classes.
  2. Step 2: Why not composition causes duplication?

    Composition leads to code duplication but improves runtime performance due to fewer indirections is incorrect; composition reduces duplication by delegating behavior.
  3. Step 3: Why not inheritance allows runtime behavior changes?

    Inheritance allows dynamic behavior changes at runtime, while composition fixes behavior at compile time is false; composition better supports runtime behavior changes via delegation.
  4. Step 4: Why not composition forces shared fields?

    Composition forces all item types to share the same data fields, limiting customization is wrong; composition allows flexible data encapsulation per type.
  5. Final Answer:

    Option B -> Option B
  6. Quick Check:

    Inheritance trades flexibility for reuse; composition trades reuse for flexibility.
Quick Trick: Inheritance = reuse but rigid; Composition = flexible but needs delegation [OK]
Common Mistakes:
MISTAKES
  • Confusing runtime flexibility between inheritance and composition
  • Assuming composition causes code duplication
  • Believing composition forces uniform data fields
Trap Explanation:
PITFALL
  • Options B, C, and D reflect common misunderstandings about OOP design trade-offs.
Interviewer Note:
CONTEXT
  • Tests candidate's grasp of OOP design principles and trade-offs in system modeling.
Master "Design a Library Management System - LLD with Relationships & Edge Cases" 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