According to the Sierra/Bates
SCJP 6 study guide (page 127), covariant returns were introduced in Java 5. But I've noticed that when compiling under Java 1.4, you can still return an object of the subclass type. So the only difference appears to be that you can
declare the subtype's override to return an object of the subclass type.
In other words, using Phil Sohar's example, the following would
not compile under 1.4, using
javac -source 1.4 Covart.java:
However, by simply changing the declared return type (but not the actual return statement), it will indeed compile under 1.4. Notice that all I changed was the return type, from
String to
Object:
So this Java padawan humbly asks, why did they bother adding this to Java 5? Is it just to save a cast in the client code?