Diamond problem in java

The diamond problem in java is coming due to multiple inheritances. It is part of the inheritance that you have learned in a separate post multiple inheritances in java. In this post, we will see what is the diamond problem in Java and Java 8? How we can resolve it? Because it is the most common question for interviews.  

What is the diamond problem in java?

As you know, multiple inheritance doesn’t support java. To achieve multiple inheritance, we can use the default method and static method in java 8.  If you haven’t read the article How to achieve multiple inheritance by inhertface yet. Then you should read it first, after that, it makes sense for you.
By use of default method in the interface, we can achieve multiple inheritances. But the diamond problem is still there. If interfaces have a common default method, Let’s see how it goes in the problem.

diamond problem in java

Also, Here we are using multiple inheritances by use of interfaces. The parent interface is University. Two interfaces, CollegeData and HostelData, extending the university interface. Both the interfaces are implementing by a concrete class StudentRecord. Hence, You can see in the image the inheritance is in a diamond shape.

Now try to find out the problem area. The parent interface University has a default method that is extended by another interface. The CollegeData and HostelData interface declares a common default method inherited by StudentRecord.
Now, these two method definitions in the StudentRecord have the same signature, which method would the StudentRecord inherit? Which method should be invoked when we try to call studentData() in StudentRecord? Therefore, It is the diamond problem.

interface University
{
	default void universityInfo()
	{
		System.out.println("University Name : KUK");
		System.out.println("Number of departments : 100");
		System.out.println("Grade of university : A");
	}
}

interface CollegeData extends University
{
	public void collegeDetail(); 
	default void studentData() 
	{
		System.out.println("courses of Student : MCA, MTECH, MBA, BCA");
	}

}

interface HostelData extends University
{
	public void hostelDetail(); 
	default void studentData()
	{
		System.out.println("Student selected on based : Percentage, Financial condition");
	}
}

public class StudentRecord implements CollegeData, HostelData 
{ 
	@Override
	public void hostelDetail() 
	{
		System.out.println("Hostel Name : RAMA");
		System.out.println("Hostel location : KUK");
	}

	@Override
	public void collegeDetail() 
	{
		System.out.println("College Name : DCSA");
		System.out.println("College Grade : A");
		System.out.println("University of College : KUK");
	}

	public static void main (String[] args) 
    { 
       StudentRecord obj = new StudentRecord();
       obj.universityInfo();
       obj.collegeDetail();
       obj.hostelDetail();
       obj.studentData();
    }
}

Output: Compilation error

How to resolve the diamond problem in java 8?

The diamond problem can be resolve manually. In this case, resolve the conflict manually by using the super keyword within the StudentRecord class to explicitly mention which method the definition wants to use. The super keyword uses to call the method of parent class/interface.

Let’s see how it works. So, here we will override the studentData() method. In which we are calling the method of parent interface by use of the super keyword.

interface University
{
	default void universityInfo()
	{
		System.out.println("University Name : KUK");
		System.out.println("Number of departments : 100");
		System.out.println("Grade of university : A");
	}
}

interface CollegeData extends University
{
	public void collegeDetail(); 
	default void studentData() 
	{
		System.out.println("courses of Student : MCA, MTECH, MBA, BCA");
	}

}

interface HostelData extends University
{
	public void hostelDetail(); 
	default void studentData()
	{
		System.out.println("Student selected on based : Percentage, Financial condition");
	}
}

public class StudentRecord implements CollegeData, HostelData 
{ 
	@Override
	public void hostelDetail() 
	{
		System.out.println("Hostel Name : RAMA");
		System.out.println("Hostel location : KUK");
	}

	@Override
	public void collegeDetail() 
	{
		System.out.println("College Name : DCSA");
		System.out.println("College Grade : A");
		System.out.println("University of College : KUK");
	}

	@Override
	public void studentData() 
	{
		HostelData.super.studentData();
		CollegeData.super.studentData();
	}

	public static void main (String[] args) 
    { 
       StudentRecord obj = new StudentRecord();
       obj.universityInfo();
       obj.collegeDetail();
       obj.hostelDetail();
       obj.studentData();
    }
}

Output: University Name: KUK
Number of departments: 100
Grade of University: A
College Name: DCSA
College Grade: A
University of College: KUK
Hostel Name: RAMA
Hostel location: KUK
Student selected on based: Percentage, Financial condition
courses of Student: MCA, MTECH, MBA, BCA

Leave a Comment