Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaDiamond Problem in Java

Diamond Problem in Java

Diamond Problem is a problem faced during Multiple Inheritance in Java. Let’s see what is Diamond Problem and how to solve this problem.

What is a Diamond Problem in Java?

Inheritance in Java is when a child class inherits the properties of the parent class. There are certain types of inheritance in Java as mentioned below:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance

Talking about Multiple inheritance is when a child class is inherits the properties from more than one parents and the methods for the parents are same (Method name and parameters are exactly the same) then child gets confused about which method will be called. This problem in Java is called the Diamond problem.

Example of Java Diamond Problem

Below is the illustration of the Diamond Problem in Java:

Java




// Java Program to demonstrate
// Diamond Problem
import java.io.*;
  
// Parent Class1
class Parent1 {
    void fun() { System.out.println("Parent1"); }
}
  
// Parent Class2
class Parent2 {
    void fun() { System.out.println("Parent2"); }
}
  
// Inherting the Properties from
// Both the classes
class test extends Parent1, Parent2 {
    // main function
    public static void main(String[] args)
    {
        test t = new test();
        t.fun();
    }
}


Output:

diamond

Explanation : In above example we have seen that “test” class is extending two classes “Parent1 ” and “Parent2” and its calling function “fun()” which is defined in both parent classes so now here it got confused which definition it should inherit.

Solution of Diamond Problem in Java

Although Diamond Problem is a serious issue but we can create a solution for it which is Interface. Interface are created by using interface keyword. It contains all methods by default as abstract we don’t need to declared as abstract ,compiler will do it implicitly. We can’t instantiate interface for this we have to use a class which will implement the interface and will write the definitions of its all functions.

Here below we will see , how can we implement multiple inheritance by interface.

Java




// Java Programs to illustrate
// use of Interface to solve
// Diamond Problem
import java.io.*;
  
// Interfaces Declared
interface Parent1 {
    void fun();
}
  
// Interfaces Declared
interface Parent2 {
    void fun();
}
  
// Inheritance using Interfaces
class test implements Parent1, Parent2 {
    public void fun()
    {
        System.out.println("fun function");
    }
}
  
// Driver Class
class test1 {
    // main function
    public static void main(String[] args)
    {
        test t = new test();
        t.fun();
    }
}


Output

fun function


Conclusion

We are able to implement multiple inheritance through interface just because in case of interface we does not provide the definition of function and when class implement that function then it write definition of function only once.

RELATED ARTICLES

Most Popular

Recent Comments