DEV Community

Cover image for Java - Diamond of Death
Mahadev K
Mahadev K

Posted on

Java - Diamond of Death

The title might be quite interesting. And people might be intrigued what’s this diamond of death... Some of you might have related this to the interfaces in java and if so, you are right! Anyway, we will see what this is all about.

In OOP we all know about one feature called inheritance. There will be a parent and then there will be a child inheriting the parent methods and attributes. The problem really comes when the child needs to inherit two or more classes and which is not possible in java. Multiple inheritance is just a “No” from Java. Why?? 🤔

diamond-of-death

From the above picture you could have understood the real issue of ambiguity for same method signature in the super classes. Poor kid couldn't get the money... Kid can also ask his grand pa right 🤔.
If you also add a class which has the same method and which acts as a grandparent for getting money then you can see the diamond formation, thus the name diamond of death.

Interfaces

So, Java introduced the so-called interfaces. Thus, the problem is solved with one simple way that a class can implement multiple interfaces. And to call the required parent’s super method,

Dad.super.getMoney(reason, amount)

Mom.super.getMoney(reason, amount)

The above example was only meant for demonstration purposes.

Interfaces really doesn’t provide any implementations. Any class that is implementing the interface should provide the implementation for the method.

Multiple implementation

Suppose two different features came and a class has to serve these two new features so the class can implement the two interfaces. One way to put this is consider our smart phone, we need a smart phone that has Wi-Fi and NFC. So, when designing the phone, we have to provide the implementations for the phone based on the features that is expected from the phone. So, here we can say that a phone that implements the NFC and Wi-Fi is the expected phone.
The cover of this document also creates the same idea.

Default keyword

Suppose a new functionality can be added to an existing interface which can be implemented by multiple classes and we cannot make all the classes to implement the method as if there are many classes to give their implementation then we can declare the new method as a default method and prioritized classes can implement that method and serving the new feature while the others will call the interfaces default method which has to handle the call in some way or throw some “UnsupportedOperationException”. I am also not sure about this feature, if anyone has more insights put down your thoughts in comments.

So, the story of interfaces and inheritance ends here... Anything you wish to share can be put in comments, happy learning 😊.

Stay safe, take care Bye-bye!

Bye bye

References

Java and Multiple Inheritance - GeeksforGeeks
java - Throw exception in interface default method - Stack Overflow

Discussion (0)