Default Method in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Lambda expressions, functional interfaces, method references, streams, Optional, and static and default methods in interfaces were all introduced in Java 8.

Default Methods in Java enable you to add new functionality to existing interfaces and ensure binary compatibility with code written for older versions of those interfaces. Default methods, in particular, allow you to extend existing interfaces with methods that accept lambda expressions as parameters.

Introduction to Default Methods in Java

Interfaces could only have abstract methods before Java 8. These methods implementation must be provided in a separate class.

If a new method is added to an interface, the implementation code for that method must be included in the class that implements the same interface.

To address this problem, Java 8 introduced default methods, which allow interfaces to include implementation-ready methods without changing the classes that implement the interface.

Syntax

Default Method in Java Example

In interfaces, Java 8 introduces a new concept called default method implementation. This feature has been provided for backward compatibility so that existing interfaces can take advantage of Java 8's lambda expression functionality. Defender methods or virtual extension methods are other names for default methods.

Output

In the above code, print() is an example of the default method.

Static Methods

Interfaces can also have static methods, which are similar to class static methods. Static methods in Java are those methods whose same copy is shared by all the objects of the class and can be called without creating a class object.

They are referenced by the class name itself or reference to the object of that class. Static methods can also be defined within the interface. Utility methods are defined using static methods. How to implement a static method in an interface is demonstrated in the following example.

Output

In the above code, print() is an example of the static method.

Default Methods and Multiple Inheritance

It's possible that a class implements two interfaces with identical default methods because of default functions in interfaces.

The implementing class should either override the default method or explicitly specify which default method should be used. The code below demonstrates how to address this ambiguity.

  1. MyClass print method call the default method of the specified interface

Output

  1. MyClass defines its own print method, which overrides the default implementation.

Output

Integrating Default Methods into Existing Libraries

Default methods allow for the addition of new functionality to existing interfaces while also ensuring binary compatibility with code written for previous versions of those interfaces as class implementing these interfaces don't have to define the default methods.

Default methods, in particular, allow the addition of methods that accept lambda expressions as parameters to existing interfaces. So these properties of default methods make it easy to add them to the pre-written existing libraries according to our needs without affecting the previous functionalities of these libraries.

More about Default Methods

  • Default methods were introduced in Java 8 to provide a way for interfaces to evolve without breaking the existing implementation classes that implement them.

  • Default methods can have implementation code, unlike abstract methods which do not have any implementation code.

  • Any class that implements an interface with a default method can either use the default implementation or provide its own implementation for that method.

  • Default methods can be overridden by the implementing class if necessary.

  • If two or more interfaces provide a default implementation for the same method, the implementing class must provide its own implementation to avoid ambiguity.

  • Default methods can be used to add new functionality to existing interfaces without breaking backwards compatibility.

  • Overall, default methods are a powerful feature that can make it easier to evolve interfaces over time without breaking existing code.

Conclusion

  • Java 8 introduced default methods, which allow interfaces to include implementation-ready methods without changing the classes that implement the interface.
  • Static methods, like static methods in classes, can be added to interfaces.
  • Default methods were created to enable backward compatibility for older interfaces, allowing new methods to be added without disrupting current code.