2

I'm trying to understand how can I overcome the "diamond problem" in JAVA let's say I have these 3 interfaces:

interface Alpha{
    public default int methodA() {
        int result=0;
        System.out.println("Print from Alpha methodA");
        return result+4;
    }
}

//**********************************

interface Betta extends Alpha {
    public default int methodA() {
        int result=0;
        System.out.println("Print from Betta methodA");
        return result+8;
    }
}
//*******************************

interface Gamma extends Alpha {
    public default int methodA() {
        int result=0;
        System.out.println("Print from Gamma methodA");
        return result+16;
    }
}

And that class:

public class Delta implements Betta,Gamma {
    public static void main(String args[]) {
    // TODO Auto-generated method stub

    Delta cObj=new Delta();
    cObj.methodA();
}

how can I get class Delta giving me the output of Alpha interface? Any other way than changing it to "Delta implements Alpha"?

SujitKumar
  • 142
  • 1
  • 10
Liana
  • 15
  • 1
  • 8

2 Answers2

0

If you are implementing Betta and Gamma in class Delta then you must be getting compilation error in Delta to override default method. Because if two interfaces have same default method then the class implementing both must define its own implementation.

You can do it like :

public class Delta implements Betta, Gamma {
    public static void main(String[] args) {
        Delta cObj = new Delta();
        cObj.methodA();
    }

    @Override
    public int methodA() {
        return Betta.super.callSuperMethodA();
    }
}

public interface Betta extends Alpha {

    public default int methodA() {
        int result = 0;
        System.out.println("Print from Betta methodA");
        return result + 8;
    }

    //Call this from Delta default method
    public default int callSuperMethodA() {
        return Alpha.super.methodA();
    }

}
Rohit Gulati
  • 542
  • 3
  • 15
  • I thought about it too but I get the ""Print from Betta methodA" and when I try to use Alpha.methodA(); inside the Betta interface I get both "Print from Betta methodA" and ""Print from Alpha methodA" @Rohit Gulati – Liana Jan 03 '17 at 12:43
  • sorry mybad, I misinterpreted your question, we cannot access it. Please refer this [link](http://stackoverflow.com/a/24280376/5803493). We will have to make it static or extend Alpha. – Rohit Gulati Jan 03 '17 at 13:51
  • Alternative: You can add one more default method in Betta where you call Alpha Default method. Updated above post. @Liana – Rohit Gulati Jan 03 '17 at 13:55
  • Thank you so much for the help! I get it now – Liana Jan 03 '17 at 14:17
0

You can either turn Alpha's method to static

public interface Alpha {
public static int methodA(){
    int result = 0;
    System.out.println("Print from Alpha methodA");
    return result+4;
}}

or you can create a method referring to Alpha's method in Betta or Gamma.

A third optional solution to this would be deleting both Gamma's and Betta's default implementations, meaning emptying both of them intirely.