Table of contents
1.
Introduction
2.
Default Method in Java Example
2.1.
Java
3.
Static Methods
3.1.
Java
4.
Default Methods and Multiple Inheritance
4.1.
Java
5.
Integrating Default Methods into Existing Libraries
6.
Frequently Asked Questions
6.1.
What is the purpose of default methods in Java? 
6.2.
Can default methods be overridden in implementing classes? 
6.3.
What is the difference between default methods and abstract methods in interfaces? 
6.4.
Can static methods in interfaces be overridden?
6.5.
How do default methods help with multiple inheritance in Java?
7.
Conclusion
Last Updated: Aug 25, 2024
Easy

Java Default Method

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.

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.

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass