Default methods
A default method is an interface method that already has a body. Implementors inherit it, and they may override it. Java added them so published interfaces can grow without breaking every class.
Detailed Explanation
Before Java 8, adding a method to List would have broken every List implementation on the planet. Default methods solved that. List.sort and Collection.stream shipped as default methods, so existing classes compiled unchanged and still gained the new behaviour.
A default method is an instance method. It can call other methods on this, including abstract ones. It cannot see instance fields, because an interface has none (only constants). Shared private helpers (Java 9+) keep default methods from copying code. Static interface methods are different: they belong to the interface type, like Comparator.comparing, and are not inherited as instance behaviour.
Resolution follows rules you need when two interfaces collide. A class wins over an interface. A subinterface wins over a superinterface. If two unrelated interfaces provide the same default signature, the implementing class must override and pick, often with A.super.method(). AbstractList and friends still matter: a concrete class can supply a better implementation than the interface default.
You reach for a default method when you are evolving an interface, or when many implementors would write the same three-line helper in terms of the abstract methods. Iterator.remove throwing UnsupportedOperationException is the historical pattern; a default can encode that common "optional operation" once.
You avoid defaults that assume too much. A default that walks the whole collection to implement removeIf is correct for Collection and disastrous if a subtype could do it in O(1) and forgets to override. Document the complexity. Keep defaults thin: they should call abstract methods, not invent policy.
The cost is a new kind of multiple inheritance, of behaviour only. Diamond problems become compile errors instead of silent choices, which is good, but the error appears at the implementor, late, when two libraries both add defaults. Treat default methods as published API with the same care as abstract ones. Changing a default body can change the behaviour of classes you do not recompile, the next time they run against the new interface.
Code Examples
interface Identified {
String id();
default String tag() {
return "[" + id() + "]"; // built only from the abstract method
}
}
class Order implements Identified {
private final String id;
Order(String id) {
this.id = id;
}
@Override
public String id() {
return id;
}
public static void main(String[] args) {
System.out.println(new Order("A-9").tag()); // prints: [A-9]
}
}interface A {
default String name() {
return "A";
}
}
interface B {
default String name() {
return "B";
}
}
class Both implements A, B {
@Override
public String name() {
return A.super.name() + "+" + B.super.name(); // must pick explicitly
}
public static void main(String[] args) {
System.out.println(new Both().name()); // prints: A+B
}
}Common Gotchas
Unrelated defaults collide
two `default String name()` methods force an override in the class. No automatic merge.
Defaults run on old classes without recompilation
changing a default in a library changes runtime behaviour for implementors who never overrode it.
A slow default becomes every implementor's cost
override it on types that can do better, and document the expected complexity.
`default` is not `public` on classes
on interfaces, default methods are still public. You cannot narrow that.