Default interface methods
Add method implementations directly in interfaces, enabling multiple inheritance of behavior.
Code Comparison
✕ Java 7
// Need abstract class to share behavior
public abstract class AbstractLogger {
public void log(String msg) {
System.out.println(
timestamp() + ": " + msg);
}
abstract String timestamp();
}
// Single inheritance only
public class FileLogger
extends AbstractLogger { ... }
✓ Java 8+
public interface Logger {
default void log(String msg) {
System.out.println(
timestamp() + ": " + msg);
}
String timestamp();
}
// Multiple interfaces allowed
public class FileLogger
implements Logger, Closeable { ... }
Why the modern way wins
Multiple inheritance
Classes can implement many interfaces with default methods, unlike single abstract class inheritance.
API evolution
Add new methods to interfaces without breaking existing implementations.
Composable behavior
Mix and match capabilities from multiple interfaces freely.
Old Approach
Abstract classes for shared behavior
Modern Approach
Default methods on interfaces
Since JDK
8
Difficulty
intermediate
JDK Support
Default interface methods
Available
Available since JDK 8 (March 2014).
How it works
Before Java 8, sharing behavior across unrelated classes required abstract classes, which limited you to single inheritance. Default methods let interfaces provide method implementations, so classes can inherit behavior from multiple interfaces. This was essential for evolving the Collections API (e.g., List.forEach, Map.getOrDefault) without breaking existing implementations.