Posts

Showing posts from March, 2017

Java 8, Default methods, Diamond Problem and the Solution!

Java 8 has introduced default methods (methods with body) in interfaces. But doesn't this introduce the diamond problem? The very problem with C, that Java solved by not having multiple inheritance. Java has given a solution to this !!!!!! If you have two interfaces with same method signature, and a class implements it.. the class will give you compilation error saying there is ambiguity. It is now the developers responsibility to solve this problem by overriding the method in its body. It can call the super interfaces method by SuperInterface.super.methodName(). Let us see an example: public class MultipleInheritanceEg { public static void main(String[] args) { C c = new C(); c.A(); } } interface A{ public default void A(){ //Some implementation }; } interface B{ public default void A(){ //Some implementation }; } interface D extends A, B{ public default void A(){ A.super.A(); }; }