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
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
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.
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.
You can also practice coding questions commonly asked in interviews on Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.