Default for Object's protected method vs Lambda(IllegalAccessError)

Dan Smith daniel.smith at oracle.com
Thu Jul 26 14:43:20 PDT 2012


A few things going on here:

1) Object.clone overrides the 'clone' method declared in any interface (default or not).  A class declaration implementing an interface with an incompatible clone should trigger an error.  Testing, I see that it does.

2) Lambda expressions implicitly define a class, so the same error you would get from a class should occur for a lambda expression.  Testing, I see the same behavior as you: the lambda expression compiles without error.  (Two test cases, both compile: a functional interface w/ a default 'clone' method, and a functional interface whose abstract method is 'clone' with return type 'String'.)

These error checks need to be specified and implemented.  Thanks for pointing the problem out.

3) Plausibly, it could be an error to declare an interface with a default method that is incompatible with an 'Object' method.  I'll make a note of this to discuss.  (Notice, however, that in Java 7 it's not illegal to declare an interface 'clone' method with an incompatible return type.)

—Dan

On Jul 25, 2012, at 9:08 PM, bitter_fox <bitterfoxc at gmail.com> wrote:

> Hi,
> In Revision:1438 of langtools, default for Object's public method was
> banned.
> But I think default for protected method should be banned either because it
> will become the cause of IllegalAccessError when we make a instance by
> lambda or others:
> 
> public class Main
> {
>    interface I
>    {
>        void f();
> 
>        /**/public/**/ Object clone() default {return null;}
>    }
> 
>    public static void main(String[] args)
>    {
>        I i = () -> {}; // preferred Object#clone(protected) implicitly
>        i.clone(); // but I can refer to I#clone
>    }
> }
> 
> This program makes IllegalAccessError:
> Exception in thread "main" java.lang.IllegalAccessError:
> Main$1.clone()Ljava/lan
> g/Object;
>        at Main.main(Main.java:14)
> 
> Regards,
> bitter_fox
> 



More information about the lambda-dev mailing list