-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
1.2.0
-
generic
-
generic
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)
======================================================================
- duplicates
-
JDK-4106143 About polymorphism: changing the return type of an overloaded method to a subcla
-
- Closed
-