JDK-4230614 : I take issue with section 8.4.6.3 of the language spec
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 1999-04-17
  • Updated: 1999-04-17
  • Resolved: 1999-04-17
Related Reports
Duplicate :  
Description

Name: gb36485			Date: 04/16/99


This thows an error

C:\WINDOWS\DESKTOP\Projects\b>type xx.java
class A {A check() { return null; }}
class B extends A { B check() { return null; }}
C:\WINDOWS\DESKTOP\Projects\b>javac xx.java
xx.java:6: The method B check() declared in class B 
cannot override the method of the same signature 
declared in class A.  They must have the same 
return type.

  B check() { return null; }
    ^

This is correct according to the jls 8.4.6.3. However, 
I think that when overriding a method, it ought to be
permissable to narrow the type of the return value. 
In the fragment above, an object of class B is always 
going also to be of type A, as A is a subclass of A.
This is in the same spirit as an overriding method
being able to specify that it does not throw errors
that a the superclass may. Restricting a subclass
is always ok.
(Review ID: 52365)
======================================================================

Comments
WORK AROUND Name: gb36485 Date: 04/16/99 This is ok: class B { A check() { return new(B); } } But it means you have to use a cast on the return value even when you know that the check function will return the narrower type: B foo = new B(); A bar = B.check(); B baz = (B) B.check(); // this cast need not be nessesary ======================================================================
11-06-2004