Search

Categories

Links

Referers

@Override Snafu

Dec 05 2006, 04:03:42 PM PST »Java»Language Comments [9]

Back in May, I described how we would update the specification and implementation of @Override. Unfortunately, I messed up when integrating the changes and the specification of @Override was not updated. The compiler's behavior is changed but the documentation does not reflect that.

I will make sure to get the documentation fixed in JDK 7 as soon as possible but it is more problematic updating the documentation of JDK 6 because JSR 270 has been finalized. In the meantime, here is what it was supposed to have looked like:

Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless either of the following conditions hold:

  • The method does override or implement a method declared in a supertype.
  • The method has a signature that is override-equivalent to that of any public or protected method declared in Object.

In case Bruce is still reading my blog, I just want to make it perfectly clear that Joe had absolutely nothing to do with this snafu.

Post a Comment:
Comments are closed for this entry.
Comments:

Yep, Still reading, I am busy on a project, but thankfully reading your blog hasn't exactly required huge amounts of time recently. Bruce

Posted by Bruce on December 05, 2006 at 08:13 PM PST #

The definition you give contradicts javac current behaviour (and IntelliJ's too) for the code below: interface I { @Override Object clone() ; } The reason as I see it is that protected methods from Object are not considered to be declared in interface (see JLS 9.2). Should you exclude protected methods in your second condition?

Posted by Eugene Vigdorchik on December 06, 2006 at 03:25 AM PST #

Snap! I'm beginning to see why Gilad wished us good luck.
You're right, I need to remove protected.

Posted by Peter von der Ahé on December 06, 2006 at 04:59 AM PST #

I want to annotate the clone() method in an interface with the Override annotation, too. So, please enhance the specification of the Override annotation in future.

Posted by Manabu Nakamura on December 07, 2006 at 11:38 PM PST #

Clone is not a member of an interface implicitly. Declaring a clone method in an interface will not replace, override, or implement any method from a supertype so it should be an error to use @Override. Consider a small example like this:
interface MyCloneable {
    public Object clone();
}
class NewClass implements MyCloneable { }
You get this error: clone() in java.lang.Object cannot implement clone() in MyCloneable; attempting to assign weaker access privileges; was public.

Posted by Peter von der Ahé on December 08, 2006 at 12:38 AM PST #

You have to redefine clone() in NewClass, don't you? Because you cahnged the visibility of clone() in MyCloneable.
interface MyCloneable {
    public Object clone() throws CloneNotSupportedException;
}

class NewClass implements MyCloneable, Cloneable {
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class NewClass2 implements Cloneable {
}

class Test {
    public static void main(String[] args) throws Exception {
        MyCloneable m = new NewClass();
        System.out.println(m.clone());
//        System.out.println(new NewClass2().clone());
//compile-time error
    }
}

Posted by Manabu Nakamura on December 08, 2006 at 03:00 AM PST #

Apologies in advance if I sounded lame...

<CODE> package java.lang; public class Object { //... protected Object clone() { //... } //... } // so this is not correct? public class MyStuff [extends java.lang.Object] { @Override protected Object clone() { //... } } // how about this? public class MyStuff { @Override public Object clone() { //... } } // and this? public class MyStuff { @Override protected MyStuff clone() { //... } } </CODE>

Posted by Alex Lam on December 11, 2006 at 12:57 AM PST #

Ugh, try again...

package java.lang;

public class Object {
    //...
    protected Object clone() {
        //...
    }
    //...
}


// so this is not correct?
public class MyStuff [extends java.lang.Object] {
    @Override
    protected Object clone() {
        //...
    }
}


// how about this?
public class MyStuff {
    @Override
    public Object clone() {
        //...
    }
}


// and this?
public class MyStuff {
    @Override
    protected MyStuff clone() {
        //...
    }
}

Posted by Alex Lam on December 11, 2006 at 12:59 AM PST #

Alex,

The second bullet is for methods in interfaces. Also, the docs now say:

unless at least one of the following conditions hold:

instead of

unless either of the following conditions hold:

Posted by Peter von der Ahé on January 04, 2007 at 07:10 AM PST #

Java is a trademark of Sun Microsystems, Inc.
Copyright © 2006,2007 Peter von der Ahé