Bird
Raised Fist0

If a new requirement arises where an interface needs to add a method without breaking existing implementations, what is the best approach to handle this in a language that originally did not support default methods in interfaces?

hard🎤 Interviewer Follow-up Q15 of Q15
OOP & Design Patterns - Abstraction - Abstract Class vs Interface - When to Use Which
If a new requirement arises where an interface needs to add a method without breaking existing implementations, what is the best approach to handle this in a language that originally did not support default methods in interfaces?
AAdd the method to the interface and require all implementers to update their code immediately.
BCreate a new interface that extends the original and adds the new method, then update clients to use the new interface.
CAdd the method as a static method in the interface.
DConvert the interface into an abstract class and provide a default implementation for the new method.
Step-by-Step Solution
  1. Step 1: Understand backward compatibility

    Adding a method directly to an interface breaks existing implementers if no default implementation exists.
  2. Step 2: Why converting to abstract class is problematic

    Changing interface to abstract class breaks existing multiple inheritance and design contracts.
  3. Step 3: Using interface extension

    Creating a new interface that extends the original preserves backward compatibility and allows gradual adoption.
  4. Step 4: Static methods in interfaces

    Static methods do not affect instance method contracts and cannot replace instance methods.
  5. Final Answer:

    Option B -> Option B
  6. Quick Check:

    Extending interfaces is the safe way to evolve contracts without breaking clients.
Quick Trick: Extend interfaces to add methods without breaking existing code.
Common Mistakes:
MISTAKES
  • Assuming all implementers can update immediately.
  • Thinking abstract classes can replace interfaces without impact.
  • Confusing static methods with instance methods in interfaces.
Trap Explanation:
PITFALL
  • Option A ignores backward compatibility; B changes design fundamentally; D misunderstands static method role.
Interviewer Note:
CONTEXT
  • Tests advanced understanding of interface evolution and backward compatibility strategies.
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