Skip to main content

Diamond problem with default methods in JDK8

No replies
Oliver789
Offline
Joined: 2012-07-26
Points: 0

Hi,

I've created 2 interfaces A and B that make use of JDK8 default methods that create the diamond problem:

interface A {

public void foo() default {
System.out.println("contents of bar: " + getBar());
}

public String getBar();
public void setBar(String bar);
}

interface B {

public void foo() default {
System.out.println("contents of bar: " + getBar());
}

public String getBar();
public void setBar(String bar);
}

Now, if I create a class Bar that implements A and B:

public class Bar implements A,B {

private String bar = "";

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}
}

Problem is that this does not compile:

Bar.java:1: error: class Bar inherits unrelated defaults for foo() from types A and B
public class Bar implements A,B {
^
Bar.java:14: error: reference to foo is ambiguous, both method foo() in B and method foo() in A match
foo();
^
2 errors

All right, I don't have a problem with this. I can understand the reason. My question is simply, whether this is the "deliberate" chosen approach to deal with the diamond problem I can rely on that it will persist or are there any plans to allow for some sort of disambiguation like:

public class Bar implements A,B {

// ...

public void bar() {
bar = "baz";
A.foo();
bar = "bazzzz";
B.foo();
}

public static void main(String[] args) {
new Bar().bar();
}
}

Thanks, Oliver