Clean • Professional
Java 8 introduced a major upgrade to interfaces: Default Methods and Static Methods.
These features made interfaces more powerful without breaking existing code.
Before Java 8, interfaces had only abstract methods, meaning no method implementation was allowed.
This created a serious problem:
👉 It caused a backward compatibility issue in large applications.
A default method is a method inside an interface that contains a complete implementation (method body).
It is used when you want to provide a default behavior to all implementing classes without forcing them to override it.
👉 It is defined using the default keyword.
Syntax
interface MyInterface {
default void show() {
System.out.println("Default Method");
}
}
Example
interface Vehicle {
default void start() {
System.out.println("Vehicle is starting...");
}
}
👉 This example provides a default implementation of start() method in the interface.
class Car implements Vehicle {
}
👉 This class does not override the default method, so it uses the interface’s implementation.
Car car = new Car();
car.start();
Output:
Vehicle is starting...
👉 This example shows that the default method works directly without overriding.
interface Payment {
default void payByUPI() {
System.out.println("Paying via UPI...");
}
}
👉 This example provides a common payment method that all implementing classes can directly use.
A static method in an interface belongs to the interface itself, not to any object or implementation class.
It is mainly used for utility or helper operations that are related to the interface but do not depend on object state.
👉 It is defined using the static keyword.
Syntax
interface MyInterface {
static void display() {
System.out.println("Static Method");
}
}
👉 This example defines a static method display() inside an interface.
Example
interface Calculator {
static int add(int a, int b) {
return a + b;
}
}
👉 This example provides a static method add() that performs addition of two numbers.
Static methods in interfaces are called using the interface name, not by creating an object.
public class Test {
public static void main(String[] args) {
System.out.println(Calculator.add(5, 3));
}
}
Output:
8
👉 This example shows that the static method add() is called using the interface name Calculator.
👉 Important Point
Java 8 introduced both default and static methods in interfaces, but they work in completely different ways.
| Feature | Default Method | Static Method |
|---|---|---|
| Belongs to | Object (instance) | Interface itself |
| Can be overridden | Yes | No |
| Called using | Object | Interface name |
| Purpose | Provide default behavior to classes | Utility/helper methods |
This example shows how default and static methods work together in a single interface.
interface Demo {
default void show() {
System.out.println("Default Method");
}
static void print() {
System.out.println("Static Method");
}
}
👉 This interface contains:
show)print)This class implements the interface but does not override any method.
class Test implements Demo {
}
👉 Since show() is a default method, it can be used directly.
public class Main {
public static void main(String[] args) {
Test obj = new Test();
obj.show(); // default method
Demo.print(); // static method
}
}
Default and static methods in interfaces are widely used in real-world Java applications to make code more flexible and reusable.
Default Methods
Default methods are used when you want to add new functionality without affecting existing code.
Static Methods
Static methods are used for utility or helper operations that are common and do not depend on object state.
Choosing between default methods and static methods depends on the purpose of your design in Java interfaces.
Use Default Methods When:
Use Static Methods When:
Default and Static methods made interfaces powerful and flexible in Java 8.
Together, they help write clean, maintainable, and modern Java code.