Evolution and Backward Compatibility Concerns
You'll learn why adding methods to interfaces was once a breaking change, and how default methods solved this problem differently than abstract classes.
The Core Problem
When you publish an interface that other developers implement, you create a contract. If you later decide to add a new method to that interface, every existing class that implements it will suddenly break—they won't have the new method, causing compiler errors.
Imagine you own a restaurant franchise. Your original contract with franchisees required them to serve breakfast and lunch. Years later, you want to add dinner service. If you require it, every existing franchise must implement it immediately or violate the contract. That's the backwards compatibility problem with interfaces.
Abstract Classes vs Interfaces: Different Evolution Stories
Abstract classes handle evolution gracefully. You can add a concrete method with a default implementation anytime. Existing subclasses inherit it automatically—no breaking changes.
Interfaces (before default methods) couldn't do this. Adding any method broke every implementation. This made interfaces fragile for library designers.
Default Methods: The Solution
Default methods (introduced in Java 8) give interfaces the same evolutionary flexibility. You can add a method with a default implementation, and existing classes inherit it without modification.
// Version 1: Original interface
interface Logger {
void log(String message);
}
// Version 2: Evolution with default method (safe!)
interface Logger {
void log(String message);
default void logError(String error) {
log("ERROR: " + error); // Default behavior
}
}
// All existing Logger implementations still compile!
Without default methods, adding logError would have broken every existing Logger implementation.
Key Takeaway: Adding methods to interfaces used to break all implementations, making interfaces rigid. Default methods solved this by allowing new methods with default behavior, giving interfaces the same evolutionary safety that abstract classes always had through concrete methods.