Introduction
Java Default Methods were introduced in Java 8 as a significant enhancement to interfaces. Before Java 8, interfaces could only contain abstract methods (along with constants). This meant that if a new method was added to an existing interface, every class implementing that interface had to provide an implementation for the new method, which could break existing applications.
To solve this problem, Java introduced Default Methods. A default method allows an interface to provide a method implementation using the default keyword. Classes implementing the interface automatically inherit the default implementation and can override it if needed.
Default methods were mainly introduced to support the evolution of the Java Collections Framework without breaking backward compatibility. For example, methods such as forEach(), stream(), and removeIf() were added to existing collection interfaces using default methods.
Consider the following interface:
interface Vehicle {
default void start() {
System.out.println("Vehicle Started");
}
}
A class implementing this interface can use the start() method without writing any additional code.
Default methods are widely used in Collections Framework, Stream API, Functional Interfaces, Enterprise Applications, and API development because they make interfaces more flexible while maintaining compatibility with existing implementations.
What are Java Default Methods?
A Default Method is a method declared inside an interface using the default keyword with a complete implementation.
Unlike abstract methods, default methods already contain executable code.
Example:
interface Animal {
default void sound() {
System.out.println("Animal makes a sound");
}
}
Any class implementing the Animal interface automatically inherits the sound() method.
Why Do We Need Default Methods?
Before Java 8, interfaces contained only abstract methods.
Example:
interface Vehicle {
void start();
}
If a new method such as stop() was added:
interface Vehicle {
void start();
void stop();
}
Every implementing class would fail to compile until it implemented the new method.
With Default Methods:
interface Vehicle {
void start();
default void stop() {
System.out.println("Vehicle Stopped");
}
}
Existing classes continue to work without modification because the interface already provides the implementation.
Features of Default Methods
Some important features are:
- Introduced in Java 8
- Declared using the default keyword
- Contain a method body
- Defined inside interfaces
- Support backward compatibility
- Can be overridden by implementing classes
- Can call other interface methods
- Used extensively in the Collections Framework
- Improve API evolution
- Support multiple inheritance of behavior
Syntax
interface InterfaceName {
default void methodName() {
// implementation
}
}
Example:
interface Greeting {
default void sayHello() {
System.out.println("Hello Java");
}
}
Example 1: Basic Default Method
interface Vehicle {
default void start() {
System.out.println("Vehicle Started");
}
}
class Car implements Vehicle {
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Output
The Car class automatically inherits the default implementation.
Example 2: Overriding a Default Method
interface Vehicle {
default void start() {
System.out.println("Vehicle Started");
}
}
class Bike implements Vehicle {
@Override
public void start() {
System.out.println("Bike Started");
}
}
public class Main {
public static void main(String[] args) {
Bike bike = new Bike();
bike.start();
}
}
Output
The implementing class overrides the default implementation.
Example 3: Multiple Default Methods
interface Printer {
default void print() {
System.out.println("Printing...");
}
default void scan() {
System.out.println("Scanning...");
}
}
class Machine implements Printer {
}
public class Main {
public static void main(String[] args) {
Machine machine = new Machine();
machine.print();
machine.scan();
}
}
Output
Scanning…
Example 4: Default Method with Abstract Method
interface Shape {
void draw();
default void display() {
System.out.println("Displaying Shape");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
circle.display();
}
}
Output
Displaying Shape
Example 5: Calling Another Interface Method
interface Message {
void show();
default void display() {
show();
}
}
class Demo implements Message {
@Override
public void show() {
System.out.println("Hello Java");
}
}
public class Main {
public static void main(String[] args) {
Demo demo = new Demo();
demo.display();
}
}
Output
Default Method Conflict
A class may implement multiple interfaces that contain the same default method.
Example:
interface A {
default void show() {
System.out.println("A");
}
}
interface B {
default void show() {
System.out.println("B");
}
}
The following class causes a compilation error:
class Test implements A, B {
}
To resolve the conflict, override the method:
class Test implements A, B {
@Override
public void show() {
A.super.show();
}
}
This explicitly calls the default method from interface A.
Default Methods vs Abstract Methods
| Feature | Default Method | Abstract Method |
|---|---|---|
| Has implementation | Yes | No |
| Uses default keyword | Yes | No |
| Can be inherited directly | Yes | No |
| Can be overridden | Yes | Yes |
| Introduced in | Java 8 | Earlier Java Versions |
Default Methods vs Static Methods
| Default Method | Static Method |
|---|---|
| Inherited by implementing classes | Not inherited |
| Can be overridden | Cannot be overridden |
| Invoked using object | Invoked using interface name |
| Uses default keyword | Uses static keyword |
Default Methods in Collections Framework
Java 8 added several default methods to collection interfaces.
Examples include:
- forEach()
- stream()
- parallelStream()
- removeIf()
- spliterator()
Example:
import java.util.*;
public class Main {
public static void main(String[] args) {
List languages =
Arrays.asList("Java", "Python", "C++");
languages.forEach(System.out::println);
}
}
Output
Python
C++
The forEach() method is a default method defined in the Iterable interface.
Real-life Example
Imagine an Online Payment System.
Imagine an Online Payment System.
interface Payment {
void pay(double amount);
default void printReceipt() {
System.out.println("Receipt Generated");
}
}
Implementations:
class CreditCardPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid: " + amount);
}
}
Every payment method automatically receives the printReceipt() functionality without duplicating code.
Other real-world applications include:
- Banking systems
- E-commerce platforms
- Reporting systems
- Notification services
- Logging frameworks
- API development
- Collections Framework enhancements
- Enterprise applications
Advantages of Default Methods
- Maintains backward compatibility
- Reduces code duplication
- Allows interface evolution
- Simplifies API maintenance
- Supports code reuse
- Improves readability
- Enables multiple inheritance of behavior
- Works seamlessly with Lambda Expressions and Stream API
Limitations of Default Methods
- Can create ambiguity when multiple interfaces define the same default method.
- Should not contain complex business logic.
- Excessive use may blur the distinction between interfaces and abstract classes.
- Overriding behavior may become confusing in large inheritance hierarchies.
Common Mistakes
1. Forgetting the default Keyword
Incorrect:
interface Demo {
void display() {
System.out.println("Hello");
}
}
This results in a compilation error.
Correct:
interface Demo {
default void display() {
System.out.println("Hello");
}
}
2. Ignoring Default Method Conflicts
If two interfaces define the same default method, the implementing class must override it to resolve ambiguity.
3. Calling a Default Method Incorrectly
Incorrect:
A.show();
Correct:
A.super.show();
Use InterfaceName.super.methodName() inside the implementing class to invoke a specific default implementation.
4. Putting Complex Business Logic in Default Methods
Default methods should provide reusable behavior, not extensive application logic.
5. Confusing Default Methods with Static Methods
Default methods are inherited by implementing classes, while static methods belong to the interface itself and are accessed using the interface name.
Best Practices
- Use default methods primarily to extend existing interfaces without breaking compatibility.
- Keep default methods focused on reusable behavior.
- Override default methods only when customization is required.
- Resolve multiple interface conflicts explicitly.
- Avoid adding heavy business logic inside interface methods.
- Use default methods together with Functional Interfaces where appropriate.
Time Complexity
A default method itself does not determine time complexity. The complexity depends on its implementation.
| Operation | Time Complexity |
|---|---|
| Method Invocation | O(1) |
| forEach() | O(n) |
| stream() | O(1) to create the stream |
| removeIf() | O(n) |
| Custom Logic | Depends on implementation |
Conclusion
Java Default Methods are a powerful feature introduced in Java 8 that allow interfaces to contain methods with implementations. They were designed primarily to support the evolution of Java APIs while maintaining backward compatibility, enabling developers to add new functionality to existing interfaces without forcing changes in all implementing classes.
By understanding their syntax, behavior, conflict resolution, best practices, and real-world applications, you can write more flexible and maintainable Java applications. Default methods, together with Lambda Expressions, Functional Interfaces, and the Stream API, are essential concepts for modern Java development and are frequently discussed in technical interviews.