Java 8 introduced several powerful features that changed the way we write code. Among these, default methods and static methods in interfaces stand out as important additions.
Before Java 8, interfaces could only contain abstract methods. With default and static methods, Java provided flexibility to add new functionality without breaking existing implementations.
In this article, we’ll explore:
– What are default methods ?
– What are static methods ?
– Their differences
– Practical examples
– Real-world usage in the JDK
What are Default Methods?
Default methods allow you to add new functionality to an existing interface without breaking the contract.
– They are declared using the `default` keyword.
– They are implicitly public.
– They can provide a common behavior across implementation classes.
– They can be overridden by the implementing classes.
Example:
interface IPayment {
void pay(double amount);
// Default method
default void printReceipt() {
System.out.println("Printing receipt...");
}
}
Implementation
class CardPayment implements IPayment {
public void pay(double amount) {
System.out.println("Payment using Card: " + amount);
}
// Overriding default method
@Override
public void printReceipt() {
System.out.println("Card Statement for September");
}
}
class UpiPayment implements IPayment {
public void pay(double amount) {
System.out.println("Payment using UPI: " + amount);
}
}
Usage:
public class Billing {
public static void main(String[] args) {
IPayment payment = new CardPayment();
payment.pay(2000);
payment.printReceipt();
payment = new UpiPayment();
payment.pay(1500);
payment.printReceipt();
}
}
Output:
Payment using Card: 2000
Card Statement for September
Payment using UPI: 1500
Printing receipt...
What are Static Methods ?
Static methods in interfaces are:
– Declared using the `static` keyword.
– Belong to the interface itself (not to instances).
– Cannot be overridden.
– Must be called using the interface name.
Example:
interface IPayment {
static void showDetails() {
System.out.println("Payment process for billing.");
}
}
Usage:
public class Billing {
public static void main(String[] args) {
IPayment.showDetails();
}
}
Difference Between Default and Static Methods
| Feature | Default | Static |
| Keyword Used | default | static |
| Belongs To | Implementation class obj | Interface |
| Overridable? | Yes | No |
| called using | implementation class obj | Interface name |
| Purpose | Add new functionality without breaking contract | Common functionality across all implementation classes |
Real-world Example – Comparator Interface
The Comparator interface in Java makes heavy use of default and static methods:
– Default methods: reversed(), thenComparing()
– Static methods: naturalOrder(), reverseOrder()
This shows how Java uses these methods to enhance old interfaces without breaking existing code.
Conclusion
– Use default methods when you want to add functionality and still give implementing classes the choice to override.
– Use static methods when you want to provide utility-like, common behavior shared across all classes.