
It is due to the conflict caused by multiple interface inheritance commonly known as the diamond problem.
#Java interface with default methods code
This code will fail to compile with the following output: java: class Class01 inherits unrelated defaults for defMethod() from types Public class Class01 implements interface01, interface02 Let’s see an example to demonstrate this situation: 1 What if a class implements two or more interfaces and all of those interfaces define the same default methods? Since classes are allowed to implement multiple interfaces in Java, this arises a question regarding multiple inheritances. By using the Java 8 default method you can easily add a default method that will not require to add its implementation in any other classes. Adding a new method will require implementing it throughout the implementation classes. In another scenario where you need to fulfill a new requirement, you will have to add a new method to an existing interface. Introducing a default method simplifies it because the implementation would be common for all classes. Why do we need to implement a default method within the interface?Ĭonsider a scenario where you have an interface that has multiple methods, and various classes are implementing this interface. now, the implementations can decide whether to implement a method or to use the default implementation. To achieve both of these objectives, developers working at Java brought in the concept of default methods in Interfaces. See Also: When To Use The Parallel Stream In Java They also aimed to provide backward compatibility by providing support for pre-existing implementations of List or Map during version upgrades. However, if you want to add new methods in a Java Interface, it has to be implemented. Later, with the introduction of Java 8 Streams, developers behind Java wanted to modify collections API to provide support for streams. Hence, the majority of the interfaces in collections API are Java Interfaces. when some of the core APIs were being written at Java, the developers used Interfaces to define API interfaces like Java Collections API. The code will present the following output: 333ĭefault Method is Executed Why default methods were introduced?Īlthough default methods in Java 8 provide a feature of having concrete methods in interfaces, it is not an improvement, but can be considered as a long due correction in Java. no implementation required for the default method Below mentioned code demonstrates an example of using an abstract method and a default method, 1 The default modifier is mandatory if you want to put a default method in an interface. The default methods have a body and default modifier that distinguishes them from a regular method. To cater to this issue, default methods allow the interfaces to have methods with implementation without affecting the classes that implement the interface. So, if a developer wanted to add a new method to an interface, then the implementation code must be provided in the class that is implementing the interface.


Developers had to provide the implementation of these methods in a separate class. Before default methods in Java 8, interfaces could only have abstract methods. The default methods were primarily introduced to resolve compatibility issues in Java. We will be further discussing all the frequently asked questions about working and using default methods in Java 8.
#Java interface with default methods how to
How to extend the interfaces that contain default methods in Java 8.Difference between default methods and regular methods.Difference between default methods and abstract class.Why do we need to implement a default method within the interface?.
