Java 8: How do default and static interface methods let you evolve an API? Show the alternatives.
Problem
PaymentGateway has 30 implementations across teams. You must add a refund operation (most gateways can reuse a generic implementation) and a shared validation helper, without breaking any implementation.
Approach 1: Add an abstract method (breaks everyone)
interface PaymentGatewayV1 {
String charge(long amountPaise);
// String refund(String chargeId); // adding this abstract method breaks all 30 implementations at compile time
}Approach 2: Abstract adapter class (the pre-Java 8 workaround)
Add the method to an abstract base class with a default body. It only helps classes that extend the adapter, and Java has single inheritance, so classes already extending something else cannot use it.
abstract class AbstractPaymentGateway implements PaymentGatewayV1 {
public String refund(String chargeId) { return "refund-requested:" + chargeId; }
}Approach 3: default method (Java 8)
Existing implementations compile unchanged and inherit the default; any gateway can override it with a native refund.
interface PaymentGateway {
String charge(long amountPaise);
default String refund(String chargeId) {
return "manual-refund-ticket:" + chargeId; // generic fallback
}
// Java 8 static helper: lives with the interface instead of a separate *Utils class
static void validateAmount(long amountPaise) {
if (amountPaise <= 0) throw new IllegalArgumentException("Amount must be positive: " + amountPaise);
}
}
class CardGateway implements PaymentGateway {
public String charge(long amountPaise) {
PaymentGateway.validateAmount(amountPaise);
return "card-charge-" + amountPaise;
}
@Override public String refund(String chargeId) { return "card-refund:" + chargeId; } // native refund
}
class UpiGateway implements PaymentGateway {
public String charge(long amountPaise) {
PaymentGateway.validateAmount(amountPaise);
return "upi-charge-" + amountPaise;
}
// no refund override: inherits the default
}Approach 4: private interface methods (Java 9) for sharing code between defaults
Defaults often share logic; Java 9 lets you keep it private inside the interface.
interface AuditedGateway {
String charge(long amountPaise);
default String chargeWithAudit(long amountPaise) { return log("charge", charge(amountPaise)); }
default String refundWithAudit(String id) { return log("refund", id); }
private String log(String op, String result) { // Java 9+
return "[audit] " + op + " -> " + result;
}
}Diamond conflicts
If a class inherits the same default from two interfaces, it must override and choose explicitly.
interface Greeter { default String hello() { return "Hello from Greeter"; } }
interface Welcomer { default String hello() { return "Hello from Welcomer"; } }
class Concierge implements Greeter, Welcomer {
@Override public String hello() { return Greeter.super.hello() + " & " + Welcomer.super.hello(); }
}
class GatewayDemo {
public static void main(String[] args) {
PaymentGateway card = new CardGateway(), upi = new UpiGateway();
System.out.println(card.charge(5000) + " " + card.refund("c1")); // card-charge-5000 card-refund:c1
System.out.println(upi.charge(2500) + " " + upi.refund("u1")); // upi-charge-2500 manual-refund-ticket:u1
System.out.println(new Concierge().hello());
AuditedGateway audited = amount -> "ok-" + amount; // still a functional interface
System.out.println(audited.chargeWithAudit(100));
try { card.charge(0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }
}
}Rules to remember
- A class method always wins over an interface default.
- A more specific interface (sub-interface) wins over a more general one.
- Otherwise the class must override and may call
X.super.method(). An interface with one abstract method plus any number of defaults is still a functional interface (that is howComparatorgainedreversed()andthenComparing).
Follow-up questions interviewers ask
Try answering each one before you open it.
What is the difference between an interface with default methods and an abstract class now?
Interfaces still cannot hold instance state or constructors, and all their members are public (apart from Java 9 private methods). Abstract classes can keep state, enforce invariants in constructors and use protected members, but a class can extend only one. Use interfaces for contracts and capabilities; use abstract classes for partial implementations that share state.
Can a default method override a method from Object such as equals or toString?
No. It is a compile error. A class always inherits equals, hashCode and toString from Object (a class method), which would always win over an interface default anyway, so allowing it would only be misleading.