Updated State of the Lambda

Brian Goetz brian.goetz at oracle.com
Mon Dec 12 13:01:29 PST 2011


> On a semantic issue, I'm a bit confused as to what happens in diamond
> inheritance situations? Specifically I'm thinking of a case similar to
> the following:
>
> interface A { void m() }
> interface B1 extends A { void m() default { System.out.println("one"); } ... }
> interface B2 extends A { void m() default { System.out.println("two"); } ... }
> class C extends B1, B2 {}

In a classic diamond:

interface A { void m() default { ... } }
interface B1 extends A {  }
interface B2 extends A {  }
class C extends B1, B2 {}

there is no problem.  A is the unique, most-specific default-providing 
interface (in the formal model, dcand(C)={A}), so we've got no problem.

If one of B1, B2 overrides m(), still no problem:

interface A { void m() default { ... } }
interface B1 extends A { void m() default { ... } }
interface B2 extends A {  }
class C extends B1, B2 {}

Here, A is pruned from consideration because B1 is more specific than A, 
so dcand(C)={B1}, and we're good.  Even if C were declared as:

class C extends B1, B2, A {}

In the example you post, it is not really a diamond (since A is not 
relevant to the inheritance), but instead simply a conflict that C must 
resolve:

interface A { /* Doesn't matter what goes here */ }
interface B1 extends A { void m() default { ... } }
interface B2 extends A { void m() default { ... } }
class C extends B1, B2 {}

Here, the compiler will insist that C override m(), because there is 
neither an m() inherited from superclasses nor a unique most-specific 
default-providing interface.  If C wants to delegate to B1 or B2, that's 
easy:

class C extends B1, B2 { void m() { B1.super.m(); } }


The inheritance story is actually pretty simple:
  - Superclasses always win over superinterfaces;
  - More specific interfaces win over less specific;
  - "Distance" in the inheritance hierarchy is unimportant;
  - If there is not a unique, most-specific default-providing interface 
that provides a concrete body, the class has to manually provide it.



More information about the lambda-dev mailing list