This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why do I need a cast? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do I need a cast?" Watch "Why do I need a cast?" New topic
Author

Why do I need a cast?

C Bryant
Greenhorn

Joined: Mar 29, 2012
Posts: 6
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
. . . or it’s a case of “if it ain’t broke, don’t fix it.”
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do I need a cast?
 
Similar Threads
3 months
Unchecked cast
how to caculate first day / last day of a week ?
Object Control Creation – From Constructor?
Doubt in Marcus Green question