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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more