Default Methods in Java8

Kiran Bhor
1 min readApr 26, 2021

If a class implements an interface, Then that class need to provide implementations for all the methods declared in an Interface. Consider the following code snippet.

This can be a trouble if we need to add a new method to the interface so as to extends the functionality of our Interface, as adding a new method to Interface will require all child classes of that interface to implement the new method.

So, To solve this problem default methods were introduced in java 8.

Consider following code snippet

So, In the above code snippet, we have added newMethod() as a default method in InterfaceParent . And though ClassChild don’t have implementation for newMethod() the code compiles without any errors.

Now we can add a new class implementing the newMethod(), or modify existing classes where the new method is required. Thus we are able to extend the functionality of the current interface without modifying the existing class ClassChild. Following code for the new class.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kiran Bhor
Kiran Bhor

Written by Kiran Bhor

Cofounded Accunity Services (Software Consultancy Firm) and Passionate Coder at Heart. reach me at kiran@accunityservices.com

No responses yet

Write a response