Default Methods in Java 8 Interfaces
Fast recall
Quick revision
Core answer
default. Implementing classes inherit it unless a class method or a more-specific override wins. Default methods give library authors a compatibility path for adding behavior to an interface, although conflicts and behavioral compatibility still need review.Understanding of Java 8's backward compatibility solution and how default methods enable interface evolution.
Not explaining why default methods were added — missing the Collections API evolution story.
Use `Collection.stream()` as the Java 8 example, and note that default methods improve interface-evolution compatibility rather than guaranteeing that every implementation remains behaviorally compatible.
Interview-ready guide
Interview answer
A default method is an instance method with a body in an interface. It lets an implementing class inherit working behavior or override it. Java 8 used this as a compatibility tool when adding APIs such as Collection.stream() and Iterable.forEach(). Adding an abstract method would have required existing implementers to supply it, while a default body gives them an implementation. The boundary is important: a new default can still conflict with another inherited default or be unsuitable for a custom implementation. Java resolves a concrete class method first, then a more-specific interface method; unrelated defaults may require an explicit override.
Default Methods in Java 8 Interfaces
Direct Answer
A default method is an instance method with a body declared in an interface using default. Implementing classes inherit it unless a class method or a more-specific override wins. Default methods give library authors a compatibility path for adding behavior to an interface, although conflicts and behavioral compatibility still need review.
Code Example
interface PaymentProcessor {
// Abstract method - must be implemented
boolean processPayment(double amount);
// Default method - inherited by all implementations
default String formatReceipt(double amount) {
return String.format("Payment of $%.2f processed successfully", amount);
}
default void logTransaction(double amount) {
System.out.println("Transaction: " + amount);
}
}
class CreditCardProcessor implements PaymentProcessor {
@Override
public boolean processPayment(double amount) {
// Custom payment logic
return amount > 0 && amount < 10000;
}
// Inherits formatReceipt and logTransaction automatically
}
class BitcoinProcessor implements PaymentProcessor {
@Override
public boolean processPayment(double amount) {
return amount > 0;
}
@Override
public String formatReceipt(double amount) {
// Custom override for crypto
return "Bitcoin payment: " + amount + " BTC";
}
}
Common Mistakes & Pitfalls
- Promising zero compatibility risk: a new default can conflict with another inherited method or provide behavior a custom implementation should replace.
- Naming the wrong JDK interface: Java 8 added
forEachtoIterable;streamandremoveIfare onCollection. - Thinking default means optional abstract method: it means the interface already supplies an implementation, subject to normal inheritance and conflict rules.
- Hiding a service workflow in an interface: dependency-heavy business orchestration often belongs in a service rather than a default method.
Check your understanding
Answer these without looking back at the article.
- Can a class override a default method?
- What happens when a class inherits conflicting default methods?
- What is the diamond problem with default methods?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.