Bird
Raised Fist0

To add a new method to an existing interface without breaking existing implementations in a language that originally did not support default methods, what is the best strategy?

hard🔁 Follow Up Q10 of Q15
OOP & Design Patterns - Abstraction - Abstract Class vs Interface - When to Use Which
To add a new method to an existing interface without breaking existing implementations in a language that originally did not support default methods, what is the best strategy?
ACreate a new interface that extends the original and adds the method.
BAdd the method directly to the existing interface and update all implementations.
CConvert the interface into an abstract class with the new method implemented.
DRemove the interface and replace it with a concrete class.
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface evolution constraints

    Adding methods breaks existing implementations if no default methods.
  2. Step 2: Use interface extension

    Create a new interface extending the old one with the new method to avoid breaking code.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Extending interfaces preserves backward compatibility [OK]
Quick Trick: Extend interfaces to add methods without breaking code [OK]
Common Mistakes:
MISTAKES
  • Directly modifying interface breaks existing implementations
  • Converting to abstract class changes design contract
  • Replacing interface with concrete class reduces flexibility
Trap Explanation:
PITFALL
  • Modifying interface directly breaks existing code; abstract class changes design.
Interviewer Note:
CONTEXT
  • Tests knowledge 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