Default methods in multiple parents

Zhong Yu zhong.j.yu at gmail.com
Sun Apr 7 08:41:13 PDT 2013


Consider this example of diamond inheritance

    interface NumberInterface
    {
        default Number foo(){ return 1; }
    }

    interface IntegerInterface extends NumberInterface
    {
        default Integer foo(){ return 2; }
    }

    static class NumberClass implements NumberInterface
    {
    }

    static class IntegerClass extends NumberClass implements
IntegerInterface
    {
    }

The code compiles and works. However I am a little uncertain. My doubt is
this: arguably, NumberClass "contains" a method `Number foo()`.
IntegerClass skips that `foo()` method in its super class and inherits a
`foo()` from a super interface, is that justifiable? Actually, if we copy
NumberInterface.foo(){} into NumberClass,

    static class NumberClass implements NumberInterface
    {
        public Number foo(){ return 1; }
    }

a seemingly innocent move, suddenly IntegerClass cannot compile anymore
(due to incompatible return types).

Could someone confirm that the original example works correctly according
to the spec? (I hope so since I'm depending on it.)

Thanks,
Zhong Yu


More information about the lambda-dev mailing list