United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-5008260 @Override should be generalized from 'superclass' to 'supertype'
JDK-5008260 : @Override should be generalized from 'superclass' to 'supertype'

Details
Type:
Enhancement
Submit Date:
2004-03-04
Status:
Closed
Updated Date:
2014-02-26
Project Name:
JDK
Resolved Date:
2011-07-21
Component:
specification
OS:
generic
Sub-Component:
language
CPU:
generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0,6
Fixed Versions:
7 (rc)

Related Reports
Relates:
Relates:
Relates:
Relates:

Sub Tasks

Description
###@###.### 2004-03-04

J2SE Version (please include all output from java -version flag):
  java version "1.5.0-beta2"
  Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b40)
  Java HotSpot(TM) Client VM (build 1.5.0-beta2-b40, mixed mode)

Does this problem occur on J2SE 1.3.x or 1.4.x?  Yes / No (pick one)
  N/A

Operating System Configuration Information (be specific):
  Windows 2000 Professional SP2

Bug Description:
  Using @Override on a method from an implemented interface causes 
  compilation to fail. This seems a shame, as it would be particularly 
  useful when overriding the method with a different return type in an 
  interface or abstract class - useful because a mistake made in doing 
  so might not cause a compiler error yet still may not be what the 
  programmer had intended.

Steps to Reproduce (be specific):

  Compile Food.java (below) using -source 1.5.  Note that removing the 
  @Override annotations allows compilation to succeed.


Test program: Food.java

public interface Food {
       Food getInstance();
}


interface Cheese extends Food {
       @Override Cheese getInstance(); // compilation error
}

class Biscuit implements Food {
       @Override public Food getInstance() { return new Biscuit(); } // compilation error
}

abstract class Fruit implements Food {
       @Override public abstract Fruit getInstance(); // compilation error
}

class Apple extends Fruit {
       @Override public Apple getInstance() { return new Apple(); } // ok
}
Elided java.net submission since compiler fixes are not accepted
for specification issues.

                                    

Comments
EVALUATION

The JLS should say:

... If a method is annotated with this annotation type a compile-time
error will occur unless either the method is override-equivalent
to a method in Object, or the method does override or implement a
method declared in a supertype.
                                     
2006-05-11
EVALUATION

The specification of java.lang.Override should be:

 * 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 the method is override-equivalent to a method
 * in {@linkplain Object}, or the method does override or implement a
 * method declared in a supertype.
                                     
2006-01-13
EVALUATION

I believe the examples in the bug should work. Nothing in the spec indicates
otherwise. There is some question if marking the initial implementation of an interface method in a class as an override is desirable.

###@###.### 2004-08-03

The FAQ in the JSR175 spec indicates that this behavior is deliberate. Far be it
from me to question the wisdom of the JSR175 EG.

###@###.### 2004-08-04
                                     
2004-08-03



Hardware and Software, Engineered to Work Together