• 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

Question about casts

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone. In the following code,


I'm surprised that each of the following two statements compiles:


Of course, each of these 2 statements will cause a ClassCastException at runtime.

I realize that the compiler has to go with the reference type. All that matters is that new Animal() is of type Animal. And you should be able to cast an Animal reference to an Animal subclass. You're telling the compiler to trust you.

Still, in this case, it seems that "new Animal()" would have to be of type Animal. I can't think of any unusual case (e.g. new Animal() { <anonymous class> } ) that would return a Cat.

So it seems to me that the compiler is following the general rule of allowing us to cast an Animal to a Cat, even though in this case, I think it would be impossible for the statement to run successfully. So maybe this is a special case that the compiler should catch?

Thanks!


 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leonard Fischer wrote:
Of course, each of these 2 statements will cause a ClassCastException at runtime.
I realize that the compiler has to go with the reference type. All that matters is that new Animal() is of type Animal. And you should be able to cast an Animal reference to an Animal subclass. You're telling the compiler to trust you.
Still, in this case, it seems that "new Animal()" would have to be of type Animal. I can't think of any unusual case (e.g. new Animal() { <anonymous class> } ) that would return a Cat.
So it seems to me that the compiler is following the general rule of allowing us to cast an Animal to a Cat, even though in this case, I think it would be impossible for the statement to run successfully. So maybe this is a special case that the compiler should catch?



Hi,

the compiler just tests the compatibility (didn't find that in the jls but it should be true) between the cast operator and the type to be casted, meaning "are they in the same hierarchical tree?". And they are of course Cat being subclass of Animal.
That's the reason for the fine compiling. But later at runtime the objects (or reference variables) are checked and SUPRISE new Animal() is not a Cat object. (DUMB compiler )

cheers
Bob
reply
    Bookmark Topic Watch Topic
  • New Topic