Defaults for Objects' methods

Rémi Forax forax at univ-mlv.fr
Thu Jul 19 02:38:20 PDT 2012


On 07/19/2012 11:20 AM, Deepak S Patwardhan wrote:
> Hi all,
>
> This has been previously discussed in the thread "defender methods and
> method of Object",
> http://mail.openjdk.java.net/pipermail/lambda-dev/2012-March/004582.html .
>
> Consider the following (which compiles with lambda build 45)
>
> public interface PrettyPrintable {
>
>      public String prettyString();
>
>      public String toString() default {
>          return prettyString();
>      }
> }
>
> Since the (current) method resolution algorithm will never result in the
> invocation of the default body of toString() from this interface, shouldn't
> the above give a compilation error with the reason being unreachable code ?
> Basically, should we disallow interfaces to provide defaults for Object's
> methods ?
>
> Regards,
> Deepak S Patwardhan.
>
>

You can access it using super,

public interface Foo extends PrettyPrintable {
    public String foo() default {
      return PrettyPrintable.super.toString();
    }
}

that said, I hope that the spec will change to make Object's public 
method overridable by default method.

Rémi



More information about the lambda-dev mailing list