• 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

Downcasting

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


The above code compiles, but fails to run throwing a ClassCastException.
Can anyone explain why is it so?

Code from Kathy bates

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

This happens because Animal is not a Dog.
The compiler does not check whether you are downcasting or upcasting, all the compiler does is to check that both the reference variables dog and animal are in the same inheritance hierarchy (which is true in this case). Therefore, you will get no compiler error.
However, when you run this, JVM will complain that you cannot cast object of Animal class to Dog, since Dog IS NOT A(n) Animal
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be easier to undersand if you compare it with :


Dog is an Animal, and casting an Animal to a Dog is not a problem, unless Animal is not a Dog In your example, animal is an Animal, not a Dog. In my example, animal is a Dog. Do you see the difference ?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudharsan Ashwin wrote:

The above code compiles, but fails to run throwing a ClassCastException.
Can anyone explain why is it so?

Code from Kathy bates



Becouse in
Animal animal = new Animal(); animal is solidly Animal
When code is coppile compiler suppose that the inheritance tree is correct but when you begin run your code ClassCastException will happen.
If you write your code like
String animal = new Animal();
you will of course get compiler error.
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Supper class can be used to refer to any sub class but whenever you are trying to cast any object the actual object inside the reference should be same as the object to whom are we casting.

In your case the actual object inside the animal reference is animal itself whenever you are trying to cast it to dog the jvm will complain because the actual object inside the animal is not same as the object in which you are trying to put this after cast (that is dog object).

Think it like 2 glasses one with half the capacity of other. You can use the bigger one to hold the water that is in smaller one. But you can not hold the water of bigger one in smaller glass (or you will loose half the water and jvm does not allow it ), But if first we put the water of smaller glass into bigger glass then we can pour the water of bigger to smaller any time.

Take the bigger as parent class and smaller as the child class.
 
Sudharsan Ashwin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I understood and your replies were very useful.
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic