Using default implementation, I can compile Illegal Extending Class which is mistaking access modifiers or return-types.

bitter_fox bitterfoxc at gmail.com
Wed Feb 8 06:21:26 PST 2012


Hi,
Using default implementation, I can compile Illegal Extending Class which
is mistaking access modifiers or return-types.

Compiler is the newest binary snapshot.

For instance:
interface I
{
    void method() default {} // method is pubilc
}

class C1
{
    void method() {} // method is package private
}

class C2 extends C1 implements I
{
    // this would be illegal, but I can compile this.
}

I i = new C2();
i.method(); // this calling causes IllegalAccessError

The other pattern:
interface I
{
    String method() default
    {
        return "str";
    }
}

class C1
{
    public Integer method()
    {
        return 0;
    }
}

class C2 extends C1 implements I
{
    // this extending would be illegal
}

C2 c2 = new C2();
C1 c1 = c2;
I i = c2;

System.out.println(c2.method()); // 0
System.out.println(c1.method()); // 0
System.out.println(i.method()); // str

Was this already known?

Regards,
bitter_fox


More information about the lambda-dev mailing list