• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why do I need a cast?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . or it’s a case of “if it ain’t broke, don’t fix it.”
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic