Bird
Raised Fist0

In a system where a class hierarchy requires both shared code and multiple unrelated capabilities, which design approach best balances flexibility and code reuse?

hard⚖️ Approach Comparison Q8 of Q15
OOP & Design Patterns - Abstraction - Abstract Class vs Interface - When to Use Which
In a system where a class hierarchy requires both shared code and multiple unrelated capabilities, which design approach best balances flexibility and code reuse?
AUse multiple abstract classes to cover shared code and capabilities.
BUse an abstract class for shared code and multiple interfaces for unrelated capabilities.
CUse only interfaces and implement all code in each class separately.
DUse a concrete base class and interfaces for capabilities.
Step-by-Step Solution
Solution:
  1. Step 1: Identify requirements

    Shared code requires inheritance; multiple unrelated capabilities require multiple inheritance of contracts.
  2. Step 2: Evaluate options

    Abstract classes allow code reuse but single inheritance only; interfaces allow multiple inheritance of contracts.
  3. Step 3: Best balance

    Combining one abstract class for shared code with multiple interfaces for capabilities balances reuse and flexibility.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Abstract class + interfaces enable code reuse and multiple capabilities [OK]
Quick Trick: Abstract class for code, interfaces for multiple contracts [OK]
Common Mistakes:
MISTAKES
  • Trying multiple abstract classes (not allowed)
  • Ignoring code reuse by using only interfaces
  • Using concrete base class limits flexibility
Trap Explanation:
PITFALL
  • Candidates often overlook single inheritance limits and misuse abstract classes for multiple capabilities.
Interviewer Note:
CONTEXT
  • Assesses ability to apply abstraction patterns in complex design scenarios.
Master "Abstraction - Abstract Class vs Interface - When to Use Which" 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