Bird
Raised Fist0

You want to provide some default behavior for subclasses but also require them to implement certain methods. Which abstraction pattern fits best?

easy🔍 Pattern Recognition Q2 of Q15
OOP & Design Patterns - Abstraction - Abstract Class vs Interface - When to Use Which
You want to provide some default behavior for subclasses but also require them to implement certain methods. Which abstraction pattern fits best?
AUse an interface with default methods for behavior and abstract methods for required ones.
BUse an abstract class with some implemented methods and some abstract methods.
CUse a concrete class and override methods in subclasses as needed.
DUse multiple interfaces to separate default and required methods.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the need for partial implementation

    Default behavior implies some methods have implementation, while others must be implemented by subclasses.
  2. Step 2: Evaluate options

    Interfaces traditionally cannot have implementations (except default methods in some languages, but limited). Concrete classes do not enforce required methods. Multiple interfaces separate contracts but do not provide default behavior.
  3. Step 3: Abstract class fits best

    Abstract classes allow partial implementation and enforce subclasses to implement abstract methods.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Abstract class supports partial implementation and required methods [OK]
Quick Trick: Abstract classes allow partial implementation with enforced methods [OK]
Common Mistakes:
MISTAKES
  • Assuming interfaces can fully provide default behavior
  • Thinking concrete classes enforce required methods
  • Believing multiple interfaces can provide default implementations
Trap Explanation:
PITFALL
  • Candidates confuse interface default methods with abstract class partial implementation, missing enforcement nuances.
Interviewer Note:
CONTEXT
  • Checks understanding of partial implementation vs pure contracts.
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