group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
Prince Rathore
Prince Rathore
Computer Science
12 Jan 2019

What is 'Diamond Problem' in Java?

Can anyone explain me Diamond problem in simple words and with an example? 

Shivangi Agrawal
Shivangi Agrawal
Information Technology
3mos ago

Diamond problem is an ambiguity arises during multiple inheritances. Java does not support multiple inheritances due to the diamond problem.

multiple inheritances are when multiple classes are inherited from the single superclass.

 Let's consider below example.

   

class A

    void fun() 

    { 

        System.out.println("A"); 

    } 


class B extends A

    void fun() 

    { 

        System.out.println("B"); 

    } 



class C extends A

    void fun() 

    { 

        System.out.println("C"); 

    } 




class Test extends B,C

   public static void main(String args[]) 

   { 

       Test t = new Test(); 

       t.fun(); 

   } 

Now when fun() is called by test object, it will cause ambiguity whether to call class A,B or C funtion.

For more explanation you can refer to this link:

https://www.journaldev.com/1775/multiple-inheritance-in-java

Multiple Inheritance in Java