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

Lambda expressions, functional interfaces, method references, streams, Optional, and static and default interface methods were 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

Before Java 8, interfaces could only have abstract methods, and their 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 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, similar to class static methods. Static methods in Java are those whose same copy is shared by all the class objects 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. The following example demonstrates how to implement a static method in an interface.

Output

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

Default Methods and Multiple Inheritance

A class may implement 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 print method, overriding the default implementation.

Output

Integrating Default Methods into Existing Libraries

Default methods allow for adding new functionality to existing interfaces while ensuring binary compatibility with code written for previous versions of those interfaces, as the class implementing these interfaces doesn'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 allow 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.

  • They 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.

  • They 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.