โ— Shell
clean mode source โ†—

Default interface methods

Code Comparison

// 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 { ... }
public interface Logger {
    default void log(String msg) {
        IO.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

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.

Related Documentation

Proof