Table of contents
1.
Introduction
2.
What Are Default Methods in Java 8?
3.
Why Were Default Methods Introduced? 
3.1.
Example:
4.
Default Method in Java Example
4.1.
Java
5.
Static Methods
5.1.
Java
6.
Default Methods and Multiple Inheritance
6.1.
Java
7.
Integrating Default Methods into Existing Libraries
8.
Default Methods vs Abstract Methods: Key Differences 
9.
Frequently Asked Questions
9.1.
What is the purpose of default methods in Java? 
9.2.
Can default methods be overridden in implementing classes? 
9.3.
What is the difference between default methods and abstract methods in interfaces? 
9.4.
Can static methods in interfaces be overridden?
9.5.
How do default methods help with multiple inheritance in Java?
10.
Conclusion
Last Updated: Jul 28, 2025
Easy

Default Method in Java 8

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java introduced default methods in interfaces with Java 8. This feature allows developers to add new methods to interfaces without breaking the existing code that implements these interfaces. Default methods provide a way to define methods with an implementation directly in the interface, ensuring backward compatibility.

Java Default Method

Default methods are particularly useful for evolving APIs. Before Java 8, adding a new method to an interface required all implementing classes to provide an implementation. This could be a hassle, especially in large codebases. With default methods, new functionality can be added smoothly.

What Are Default Methods in Java 8?

Default methods were introduced in Java 8 to allow interfaces to have method implementations without breaking existing implementations. Prior to Java 8, interfaces could only have abstract methods. With default methods, developers can add new functionality to interfaces while maintaining backward compatibility with classes that already implement them.

Why Were Default Methods Introduced? 

Before Java 8, interfaces could only have abstract methods. This created a major problem: if you wanted to add a new method to an existing interface, all implementing classes had to provide an implementation—breaking existing code and causing maintenance issues.

To solve this, default methods were introduced. They allow developers to add new methods with a default implementation in interfaces, enabling backward compatibility. This means older classes can still compile and run without needing to implement the new method.

Example:

interface Logger {
    default void log(String message) {
        System.out.println("Log: " + message);
    }
}

Now, any existing class implementing Logger gets the log() method automatically, unless it overrides it.

Default methods support interface evolution in a clean, non-breaking way—crucial for API and library design where preserving compatibility is essential.

Default Method in Java Example

Let's look at a simple example to understand default methods better.

  • Java

Java

interface Vehicle {

   // Default method

   default void print() {

       System.out.println("I am a vehicle.");

   }

}

class Car implements Vehicle {

   public void print() {

       System.out.println("I am a car.");

   }

}

class Bike implements Vehicle {

   // No need to implement print() method

}

public class Main {

   public static void main(String[] args) {

       Car car = new Car();

       car.print();

       Bike bike = new Bike();

       bike.print();

   }

}
You can also try this code with Online Java Compiler
Run Code


Output: 

I am a car.
I am a vehicle.


In this example, the Vehicle interface has a default method print(). The Car class overrides this method, while the Bike class uses the default implementation provided by the interface.

Static Methods

Along with default methods, Java 8 also introduced static methods in interfaces. Static methods in interfaces are similar to static methods in classes. They belong to the interface and cannot be overridden in implementing classes.

Here is an example:

  • Java

Java

interface Vehicle {

   default void print() {

       System.out.println("I am a vehicle.");

   }

   static void blowHorn() {

       System.out.println("Blowing horn!");

   }

}

public class Main {

   public static void main(String[] args) {

       Vehicle.blowHorn()

   }

}
You can also try this code with Online Java Compiler
Run Code

Output: 

Blowing horn!


In this example, blowHorn() is a static method in the Vehicle interface. It can be called directly using the interface name.

Default Methods and Multiple Inheritance

One of the primary motivations for introducing default methods was to handle multiple inheritance issues in Java. With default methods, a class can implement multiple interfaces with overlapping method signatures without running into conflicts.

However, if two interfaces provide different default implementations for the same method, the implementing class must resolve the conflict by overriding the method.

  • Java

Java

interface Vehicle {

   default void print() {

       System.out.println("I am a vehicle.");

   }

}

interface FourWheeler {

   default void print() {

       System.out.println("I am a four-wheeler.");

   }

}

class Car implements Vehicle, FourWheeler {

   public void print() {

       Vehicle.super.print();

       FourWheeler.super.print();

       System.out.println("I am a car.");

   }

}

public class Main {

   public static void main(String[] args) {

       Car car = new Car();

       car.print();

   }

}
You can also try this code with Online Java Compiler
Run Code

Output

I am a vehicle.
I am a four-wheeler.
I am a car.


In this example, the Car class implements both Vehicle and FourWheeler interfaces. It overrides the print() method to resolve the conflict between the two default methods and call both implementations.

Integrating Default Methods into Existing Libraries

Default methods are instrumental in evolving interfaces within existing libraries. By using default methods, library developers can add new functionality without breaking existing client code. This approach ensures a smoother transition and backward compatibility.

Consider an existing interface in a library:

interface LibraryInterface {
    void existingMethod();
}


To add a new method without default methods, all implementing classes would need to be updated. With default methods, the library can evolve seamlessly:

interface LibraryInterface {
    void existingMethod();
    // New default method
    default void newMethod() {
        System.out.println("New method in LibraryInterface");
    }
}


Now, existing implementations of LibraryInterface can continue to work without modification, and new implementations can override the newMethod() if needed.

Default Methods vs Abstract Methods: Key Differences 

Understanding the difference between default and abstract methods in Java is essential, especially when working with interface evolution or designing flexible APIs.

FeatureDefault Method (Java 8)Abstract Method
Implementation Required?No – comes with a method bodyYes – must be implemented by subclass
Declared InInterface (using default keyword)Interface or abstract class
Supports Multiple Inheritance?Yes, but may require resolution in case of conflictsNo – implemented by a single class
Use in API DesignIdeal for evolving APIs without breaking implementationsForces implementation of essential behaviors
Syntaxdefault void methodName() {}void methodName();

Frequently Asked Questions

What is the purpose of default methods in Java? 

Default methods allow adding new methods to interfaces without breaking the existing code that implements these interfaces. They provide a way to evolve APIs while maintaining backward compatibility.

Can default methods be overridden in implementing classes? 

Yes, implementing classes can override default methods to provide their own implementation.

What is the difference between default methods and abstract methods in interfaces? 

Default methods have a body and provide a default implementation, while abstract methods do not have a body and must be implemented by the implementing classes.

Can static methods in interfaces be overridden?

No, static methods in interfaces belong to the interface and cannot be overridden in implementing classes.

How do default methods help with multiple inheritance in Java?

Default methods allow a class to implement multiple interfaces with overlapping method signatures. The class can provide its own implementation to resolve conflicts between different default methods.

Conclusion

Default methods in Java are a powerful feature introduced in Java 8 to provide backward compatibility and ease of evolution for interfaces. They enable developers to add new functionality to interfaces without breaking existing implementations. Along with static methods, default methods enhance the flexibility and capability of Java interfaces. Understanding and utilizing these features can lead to more robust and maintainable code.

Live masterclass