Java - InterfacesHow 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.Check Answer
Step-by-Step SolutionSolution:Step 1: Understand interface evolutionAdding abstract methods breaks existing implementations as they must implement new methods.Step 2: Use default methods for backward compatibilityDefault methods provide a body, so existing classes are not forced to implement them.Final Answer:Add a new default method with a body to the interface. -> Option AQuick 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 classesThinking interface methods cannot have bodies
Master "Interfaces" in Java9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Java Quizzes Abstraction - Abstract classes - Quiz 12easy Classes and Objects - Instance methods - Quiz 15hard Classes and Objects - Object interaction - Quiz 13medium Classes and Objects - Object interaction - Quiz 5medium Constructors - Default constructor - Quiz 15hard Constructors - This keyword usage - Quiz 11easy Custom Exceptions - Best practices - Quiz 1easy Exception Handling - Checked vs unchecked exceptions - Quiz 4medium Inheritance - Why inheritance is used - Quiz 6medium Interfaces - Static methods in interfaces - Quiz 15hard