Default methods in Interfaces in Java 8

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at a program to demonstrate default methods in Interfaces in Java 8.

  • Default methods are introduced in Java 8 to provide a default implementation for methods in an Interface.
  • Using this feature, new functionality can be added to the existing interfaces without breaking the code.
  • “default” keyword is specified while providing the implementation for a method in an Interface.

Let’s look at an example program:

Output:

Instead of specialized behavior, if the child interface wants to use its parent default method implementation, below syntax is used:

<ParentInterfaceName>.super.<methodName>();

So if we comment the line 32 and uncomment line 31 in the above program, the output would be:

Happy Learning :).

Please leave a reply in case of any queries.

Leave a Reply

Your email address will not be published. Required fields are marked *