Bird
Raised Fist0

You want to add a new method to an existing interface without breaking existing implementations. How can default interface methods help?

hard🚀 Application Q8 of Q15
C Sharp (C#) - Interfaces
You want to add a new method to an existing interface without breaking existing implementations. How can default interface methods help?
ABy removing all existing methods and replacing with new ones.
BBy providing a default implementation, existing classes don't need to implement the new method.
CBy forcing all classes to implement the new method immediately.
DBy making the interface abstract so no implementation is needed.
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface versioning problem

    Adding new methods to interfaces usually breaks existing classes that don't implement them.
  2. Step 2: Use default interface methods to solve this

    Providing a default implementation allows existing classes to continue working without changes.
  3. Final Answer:

    By providing a default implementation, existing classes don't need to implement the new method. -> Option B
  4. Quick Check:

    Default methods enable interface evolution = C [OK]
Quick Trick: Default methods prevent breaking changes in interfaces [OK]
Common Mistakes:
MISTAKES
  • Thinking all classes must update immediately
  • Believing interfaces can remove methods safely
  • Confusing abstract interfaces with default methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes