Inheritance and Diamond Problem in Java

Salitha Chathuranga
4 min readJul 11, 2022

--

Lets learn all about inheritance

Hi guys! Today we can discuss one of the concepts we have in OOP in Java. Actually this was asked from me during an interview also. So, I have decided to write the things I know already. But there will be more facts just beyond that explaining Inheritance!! More focus will be on Diamond problem and what are the alternative ways to solve it…

What is Inheritance?

Inheritance is a relation/association between two classes where one class inherits the properties of the other class. This relation is defined using the extends keyword in Java like this.

public class X extends Y {}

This is IS-A relationship. Let’s take an example. We can define Vehicle and Car as two related classes where Vehicle is the parent class and Car is the child. Here, Car IS-A Vehicle!!! Isn’t it?

I won’t be deeply explaining about inheritance. But we can briefly discuss about inheritance types we have.

  • Single Inheritance — Extend a class only by 1 class
public class X {}public class X extends Y {}
  • Multi Level Inheritance — Extend a class from a derived class, followed by an order of association
public class X {}public class Y extends X {}
public class Z extends Y {
}
  • Hierarchical Inheritance — Extend set of classes with the same parent class
public class X {}public class Y extends X {}public class Z extends X {}

Multiple Inheritance

This is the focus of the article!!! Here 1 class is extended by more than 1 classes.

public class X {}public class Y {}public class Z extends X, Y {}

Hold on! Is this supported by Java? No! Multiple inheritance is not allowed in Java! At any given point in time, a given Java class can extend from only one super class.

Why? Let’s see…

Diamond Problem

It’s happening in this scenario. Let’s imagine we have a abstract Parent class called A. Then We have X and Y classes which extend A. Right? Then we have another class Z which extends X and Y both(Tricky part)! All the classes are having a method called methodX which is inherited from A. X and Y both have overridden the method implementation to provide their own logic.

So, how to override methodX inside class Z????

We cannot. It is creating an ambiguity because, we have 2 parent classes which have the same identical method. So, which one to override? Java compiler itself comes to a confusion! This is why Java does not support for multiple inheritance. There we have faced the diamond problem!

So, why it is called as diamond problem?

Let me show you a UML diagram. See the class arrangement…Then you will realize!

diamond problem scenario

You got it right? The diamond shape class setup is the reason for this name!

Alternative Solution

We have similar kind of alternative implementation with interfaces. We can implement a class with any number of interfaces. It is already supported in Java.

But what happens if two interfaces are also having the same identical method?

Look at the below example…This includes Java 8 default methods — if you do not know about that, please read here: https://blog.devgenius.io/interface-changes-in-java-8-feae3ab6970d

We have 2️⃣ interfaces with the same default method. Then Z is the derived class from inheritance Now it has faced ambiguity in methodX() method.

interface X {  
default void methodX() {
System.out.println("X:::methodX");
}
}
interface Y {
default void methodX() {
System.out.println("Y:::methodX");
}
}
public class Z implements X, Y {
public void methodX() {
// what to do here???
}
public static void main(String args[]) {
Z z = new Z();
z.methodX();
}
}

There are 2️⃣ ways to handle this.

  1. Override the class method with own logic
  2. Override the class method with one of the inherited interface implementation

✅ Override: provide own logic

public class Z implements X, Y {
public void methodX() {
System.out.println("ZClass:::methodX");
}
public static void main(String args[]) {
Z obj = new Z();
obj.methodX();
}
}

✅ Override: provide inherited class implementation

public class Z implements X, Y {
public void methodX() {
X.super.methodX();
// OR
Y.super.methodX();
}
public static void main(String args[]) {
Z obj = new Z();
obj.methodX();
}
}

This way we can get rid of ambiguity in the common method, as per our requirement — either method 1 or 2…

This is all about diamond problem in inheritance Java. We have discussed an alternative way of solving this kind of situation also…So, now it’s time to wrap up!!! Read and share your thoughts here..

Bye Bye!

--

--

Salitha Chathuranga
Salitha Chathuranga

Written by Salitha Chathuranga

Associate Technical Lead at Sysco LABS | Senior Java Developer | Blogger

Responses (1)