Java doesn't have "Diamond" problem : Really ..?

Java doesn’t have “Diamond” problem : Really ..?

We have been confidently telling that Java doesn’t have diamond problem even if it support multiple inheritance.

In object-oriented programming languages with multiple inheritance and
knowledge organization, the diamond problem is an ambiguity that
arises when two classes B and C inherit from A, and class D inherits
from both B and C.

But do you that the diamond problem is now there in newly introduced Java 8. It is because of the introduction of default methods in Interface. In Java8 Interfaces not only have method declaration but their default implementation. Any class implementing can either use default implementation from interface or provide its own implementation. Since Java class can implement multiple interfaces it is possible that two interfaces has same default methods, that brings diamond problem.

How do we overcome diamond problem ?

Consider following code snippet

public interface InterfaceA {
    default long calculateArea(long height, long width) {
        return height * width;
    }
}
public interface InterfaceB {
    default long calculateArea(long height, long width) {
        return height * width;
    }
}
public class ImplClass implements InterfaceA, InterfaceB {

    public static void main(String[] args) {
        ImplClass obj = new ImplClass();
        System.out.println(obj.calculateArea(10, 10));
    }
}

Trying to compile this code, Java8 compiler throws errors

Duplicate default methods named calculateArea with the parameters
(long, long) and (long, long) are inherited from the types InterfaceB
and InterfaceA

Java8 compiler forces the implementing class to choose either of default implementations from Interfaces or provide its own implementation. The final code would be

public class ImplClass implements InterfaceA, InterfaceB {
    /* Compiler forces to provide implementation for ambiguous 
     * default methods from multiple imterfaces */
    public long calculateArea(long height, long width) {
        /*Programmer have option to either select one the default 
         * implementation
         * */
        //return InterfaceA.super.calculateArea(height, width);
        //OR
        //return InterfaceB.super.calculateArea(height, width);
        //OR
        //Own implementation 
        return height * width * 2;
    }
    public static void main(String[] args) {
        ImplClass obj = new ImplClass();
        System.out.println(obj.calculateArea(10, 10));
    }
}

Why do we need default methods…?

A default method is a method declaration and implementation, and it is
denoted with the default keyword preceding the declaration. Any class
that extends an interface containing a default method can choose to
implement and override the default implementation or simply use the
default implementation.

The default methods are being introduced to have additional methods in existing without breaking existing code base ( backward compatibility) .You can to add a new method to an existing interface without breaking backward compatibility with other code.Develop a default method (a.k.a. defender method) within the interface so that any classes implementing the interface are not required to provide an implementation of the new method.

Why now … ?

You must be thinking why do Java 8 needs to introduce the default methods, although this problem is there from very version of Java. The main motive could be introduction of Lambda expressions

lambda expressions are a convenient way to create anonymous functions.
They provide an easy way to create a single method interface using an
expression or series of statements. Lambda expressions are built upon
functional interfaces, which are interfaces that contain a single
abstract method.They can be applied in many different contexts, ranging from simple anonymous functions to sorting and filtering Collections. Moreover, lambda expressions can be as signed to variables and then passed into other objects.

Default methods were added in order to provide a way to change interfaces without breaking backward compatibility. Many interfaces needed to be changed to accommodate Java 8 API updates, and in doing
so, these interfaces would possibly break functionality of code that was written for prior releases of Java. By adding default methods to interfaces, engineers were able to add new functionality to existing interfaces without breaking the code that relied on those interfaces.

The main purpose of this post is to introduce ‘Default methods’ in Java8, but I though it would be interesting to start with this article with consequences. It is very fun fact that one of the base we defend Java over other OOPS languages like C++, it no more valid :)
So keep in mind that Java is not diamond problem proof.

Comments

  1. Jikage Rising Mod is a new and unique strategy game for Android that has quickly become a favorite. The goal of the game is to control as many territories as possible by collecting gold, build up your army, and crush your opponents.

    ReplyDelete

Post a Comment

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester