• Post Reply Bookmark Topic Watch Topic
  • New Topic

Questio about castdown and ClassCastException (Certification)  RSS feed

 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles ok



But why I got the exception below when I try to run it?



Is something related to Animal vtable not being filled up with Dog addresses?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting an object reference never changes the object in any way; it just tells the compiler "I know for a fact that the object being referred to is actually of some more specific type." If a cast is a lie -- as it is here! -- then you get an exception at runtime.

To be a little more specific: the line

Dog d = (Dog) animal;

means "I know that 'animal' is referring to an instance of Dog -- an object that was created at some point using 'new Dog()'." But since this is not true -- the object is in fact an instance of Animal, created using 'new Animal()' -- the JVM complains at runtime.

Note that this is perfectly legal code:

Animal animal = new Dog();
Dog d = (Dog) animal;

because here the cast is telling the truth!
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valmir Cinquini wrote:
Is something related to Animal vtable not being filled up with Dog addresses?


you should seek the answer at the Java language level, and not bother about how things may be implemented.

The first thing to understand is that a Dog variable cannot hold any other object than a Dog object or a Dog subtype object (which also is a Dog object). It cannot hold an Animal object for for the simple reason that Animal isn't a subtype of Dog. It's the other way around.

What you do here is that you lie to the compiler. By explicitly casting you forcefully exert your will claiming that the Animal variable holds a Dog object although it doesn't. The compiler has to take your word for it because it cannot really know at compiletime. The Animal variable in fact could hold a Dog object as you say it does. But at runtime you get exposed. You had lied indeed and your punishment is that your program will fail miserably with an exception.
 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulrika Tingle wrote:
Valmir Cinquini wrote:
Is something related to Animal vtable not being filled up with Dog addresses?


You should seek the answer at the Java language level, and not bother about how things may be implemented.

The first thing to understand is that a Dog variable cannot hold any other object than a Dog object or a Dog subtype object (which also is a Dog object). It cannot hold an Animal object for for the simple reason that Animal isn't a subtype of Dog. It's the other way around.

What you do here is that you lie to the compiler. By explicitly casting you forcefully exert your will claiming that the Animal variable holds a Dog object although it doesn't. The compiler has to take your word for it because it cannot really know at compiletime. The Animal variable in fact could hold a Dog object as you say it does. But at runtime you get exposed. You had lied indeed and your punishment is that your program will fail miserably with an exception.


OK, when doing the exam, should I assume that castdown doesnt work? In which cincustances castdown would work? Could you giveme a sample? This is still a little bit cloudy for me...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!