Bird
0
0

How can you use default methods to add new functionality to an existing interface without breaking existing implementations?

hard📝 Application Q9 of 15
Java - Interfaces
How can you use default methods to add new functionality to an existing interface without breaking existing implementations?
AAdd a new default method with a body to the interface.
BCreate a new interface extending the old one.
CRemove all existing methods and add new ones.
DAdd a new abstract method to the interface.
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface evolution

    Adding abstract methods breaks existing implementations as they must implement new methods.
  2. Step 2: Use default methods for backward compatibility

    Default methods provide a body, so existing classes are not forced to implement them.
  3. Final Answer:

    Add a new default method with a body to the interface. -> Option A
  4. Quick Check:

    Default methods enable interface evolution without breaking code [OK]
Quick Trick: Use default methods to add features without breaking code [OK]
Common Mistakes:
  • Adding abstract methods breaks existing classes
  • Thinking interface methods cannot have bodies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes