GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Default interface methods | java.evolved
Language
Intermediate
Default interface methodsAdd 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) {
IO.println(
timestamp() + ": " + msg);
}
String timestamp();
}
// Multiple interfaces allowed
public class FileLogger
implements Logger, Closeable { ... }
See a problem with this code? Let us know.
Why the modern way wins
🔀
Multiple inheritanceClasses can implement many interfaces with default methods, unlike single abstract class inheritance. 📦
API evolutionAdd new methods to interfaces without breaking existing implementations. 🧩
Composable behaviorMix 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
Proof
Related patterns
Language
Intermediate
Private interface methodsJava 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));
}
}
JDK 9+
→
Language
Beginner
Pattern matching for instanceofJava 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'
}
JDK 16+
→
Language
Intermediate
Sealed classes for type hierarchiesJava 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 {}
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. |