GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Default interface methods | java.evolved

Home / Language / Default interface methods
Language Intermediate

Default interface methods

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Add method implementations directly in interfaces, enabling multiple inheritance of behavior.

Interfaces
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) {
        IO.println(
            timestamp() + ": " + msg);
    }
    String timestamp();
}

// Multiple interfaces allowed
public class FileLogger
    implements Logger, Closeable { ... }
Share 𝕏
See a problem with this code? Let us know.
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.

Related Documentation
Evolving Interfaces (dev.java) ↗
Proof
View proof source ↗
Related patterns
Language Intermediate

Private interface methods

Java 8
interface Logger {
    default void logInfo(String msg) {
        System.out.println(
            "[INFO] " + timestamp() + msg);
    }
    default void logWarn(String msg) {
        System.out.println(
            "[WARN] " + timestamp() + msg);
    }
}
Java 9+
interface Logger {
    private String format(String lvl, String msg) {
        return "[" + lvl + "] " + timestamp() + msg;
    }
    default void logInfo(String msg) {
        IO.println(format("INFO", msg));
    }
    default void logWarn(String msg) {
        IO.println(format("WARN", msg));
    }
}
Hover to see modern ➜
JDK 9+ →
Language Beginner

Pattern matching for instanceof

Java 8
if (obj instanceof String) {
    String s = (String) obj;
    int length = s.length();
    // do something with 'length'
}
Java 16+
if (obj instanceof String s) {
   int length = s.length();
    // do something with 'length'
}
Hover to see modern ➜
JDK 16+ →
Language Intermediate

Sealed classes for type hierarchies

Java 8
// Anyone can extend Shape
public abstract class Shape { }
public class Circle extends Shape { }
public class Rect extends Shape { }
// unknown subclasses possible
Java 17+
public sealed interface Shape
    permits Circle, Rect {}
public record Circle(double r)
    implements Shape {}
public record Rect(double w, double h)
    implements Shape {}
Hover to see modern ➜
JDK 17+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.