Defaults for Objects' methods
Deepak S Patwardhan
deepak.patwardhan at itaas.com
Thu Jul 19 07:14:02 PDT 2012
Hello,
The problem with the PrettyPrintable interface is that the output of the
following isn't pretty.
public class PrettyObject implements PrettyPrintable {
public String prettyString() {
return "Pretty Object";
}
public static void main(String[] args) {
System.out.println(new PrettyObject());
}
}
So, I believe we have to make a choice.
1. Make the output pretty. Add special handling for Objects' methods at
runtime. (to put it in very brief words)
2. Make it a compilation error. Add special handling for Object's methods at
compile time.
Regards,
Deepak S Patwardhan.
-----Original Message-----
From: lambda-dev-bounces at openjdk.java.net
[mailto:lambda-dev-bounces at openjdk.java.net] On Behalf Of Rémi Forax
Sent: 19 July 2012 15:08
To: lambda-dev at openjdk.java.net
Subject: Re: Defaults for Objects' methods
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