☕ Java Intermediate

What are default methods in Java interfaces?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

Default methods, introduced in Java 8, allow interfaces to provide concrete method implementations. Before Java 8, adding a new method to an interface would break all implementing classes. Default methods solve the backwards-compatibility problem — you can add new methods to interfaces without breaking existing implementations. Declare with the default keyword: default void log(String message) { System.out.println(message); }. Implementing classes can override default methods. If a class implements two interfaces with the same default method, the class must override it to resolve the ambiguity. Default methods enable evolution of interfaces over time — they are the reason Java could add stream-related methods to existing collection interfaces.

Pro Tip

This topic has Java-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.