This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You cannot have two methods in the same class with signatures that only differ by return type. Until the J2SE 5.0 release, it was also true that a class could not override the return type of the methods it inherits from a superclass. What this means is that a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass. This feature removes the need for excessive type checking and casting.
Watch out! This is only true for reference types; not true for primitive types! Thus if a method returns an int, in superclass, and its overrided version, in the subclass, declares short to be the return-type, you will get an error. This is explained in the JLS as follows:-
Ch.9, Section 9.4.1 "If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type-substitutable (�8.4.6) for d2, or a compile-time error occurs."
Ch.8, Section 8.4.5 "A method declaration d1 with return type R1 is return-type-substitutable for another method d2 with return type R2, if and only if the following conditions hold:-
If R1 is a primitive type, then R2 is identical to R1.
If R1 is a reference type then:
(i) R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by unchecked conversion (�5.1.9), or (ii) R1 = | R2 |.
If R1 is void then R2 is void.
Regards, Abdul Rehman. [ November 21, 2006: Message edited by: Abdul Rehman ]