Forums Register Login
Why do I need a cast?
Don't understand why I should need a cast in this case?

Seems like the Calendar should be smart enough to know that it has a clone() method. I would have expected start.clone() to return an object of type Calendar, not of type Object.
The clone() method is declared on Object to return type Object. Up until 1.5, there were no covariant return types, so if the parent class's method is declared to return type X, the child's method must also be declared to return type exactly type X.

Starting with 1.5, covariant return types allow the child method to declare a return of X or any subtype of X. Calendar's clone() method would therefore be allowed to declare a return type of any subtype of Object (with Calendar being the obvious choice), but apparently nobody bothered to go back in and change its declaration.
. . . or it’s a case of “if it ain’t broke, don’t fix it.”
Won't you be my neighbor? - Fred Rogers. tiny ad:

This thread has been viewed 675 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 21, 2018 20:17:41.