Interface Default Methods in Java 8 [Resources to Follow]
More details : https://dzone.com/articles/interface-default-methods-java
Excellent Article from : https://muhammadkhojaye.blogspot.com/2014/03/interface-default-methods-in-java-8.html
Java 8 introduces “Default Method” or (Defender methods) new feature, which allows developer to add new methods to the interfaces without breaking the existing implementation of these interface. It provides flexibility to allow interface define implementation which will use as default in the situation where a concrete class fails to provide an implementation for that method.
Let consider small example to understand how it works:
Reengineering an existing JDKframework is always very complex. Modify one interface in JDKframework breaks all classes that extends the interface which means that adding any new method could break millions of lines of code. Therefore, default methods have introduced as a mechanism to extending interfaces in a backward compatible way.
Default methods can be provided to an interface without affecting implementing classes as it includes an implementation. If each added method in an interface defined with implementation then no implementing class is affected. An implementing class can override the default implementation provided by the interface.
For Java 8, the JDKcollections have been extended and forEachmethod is added to the entire collection (which work in conjunction with lambdas). With conventional way, the code looks like below,
The same mechanism has been used to add Stream in JDKinterface without breaking the implementing classes.
When to Use Default Method Over Abstract Classes
Abstract classes versus interfaces in Java 8
After introducing Default Method, it seems that interfaces and abstract classes are same. However, they are still different concept in Java 8.
Abstract class can define constructor. They are more structured and can have a state associated with them. While in contrast, default method can be implemented only in the terms of invoking other interface methods, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.
Default Method and Multiple Inheritance Ambiguity Problems
Since javaclass can implement multiple interfaces and each interface can define default method with same method signature, therefore, the inheritedmethods can conflict with each other.
We can choose any default implementation or both as part of our new method.
Difference Between Default Method and Regular Method
Default Method is different from the regular method in the sense that default method comes with default modifier. Additionally, methods in classes can use and modify methodarguments as well as the fields of their class but default method on the other hand, can only access its arguments as interfaces do not have any state.
In summary, Default methods enable to add new functionality to existing interfaces without breaking older implementation of these interfaces.
When we extend an interface that contains a default method, we can perform following,
Not override the default method and will inherit the default method.
Override the default method similar to other methods we override in subclass..
Redeclare default method as abstract, which force subclass to override it.