• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException  RSS feed

 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I didnt not understand why there should be a ClassCastException for this code? can someone please explain me about this..


Thanks in advance
 
saurav sarkar
Ranch Hand
Posts: 180
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asha,

I had the same question.Please see This thread.

My query was nicely explained by Ranchers.
Hope it helps you.
 
John Melton
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you're not instantiating a Dog class. You're instantiating an Animal class. If you tried in the other direction, it would work. Think about it: all animals are not necessarily dogs, but all dogs are animals. If you're line was Animal a = new Dog();, then you wouldn't have a problem.
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !!!

the problem is at this line:
Dog d =(Dog)a;

The code will compile but will throw an exception at runtime.

You cannot cast an Animal to a Dog. But you can cast Dog to an animal.

If you have B extends A then B is a A.
Any Animal is not a Dog, but any Dog is an Animal.
[ August 15, 2007: Message edited by: Collins Mbianda ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to offer a little refinement....

It's my understanding that you can pretty much cast anything to anything else. You are just telling the compiler to believe you!

When you do that, it can get you at run time and here is why it got you...

You cast an animal type to a dog type and assigned it to a Dog reference. When you reference an object, the compiler will attempt to use methods defined by the REFERENCE TYPE NOT the OBJECT that it refers to. Therefore I believe that, at RUNTIME, the JVM had your Dog type reference, d, and you told it to invoke d.display(). It is true that both objects have a display() method but, part of polymorphism is that, at RUNTIME, the JVM decides which type of method, animal or dog, to call based on the reference type NOT the type of object that it refers to. Therefore, the JVM has a Dog reference so it believes that it should be refering to a Dog display() method and an Animal object doesn't have one!

And that is what is giving you the exception.

I'd bet that if you changed

Dog d = (Dog) a; to

Animal d = (Dog) a;

it would compile, run, and print out the Animal version of the display routine. Because, even though you have cast it as a Dog, the JVM will see the animal reference and call the animal version of display().
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bob Ruth:
I'd bet that if you changed


How much? I'll take that bet.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bob Ruth:
... It's my understanding that you can pretty much cast anything to anything else. You are just telling the compiler to believe you! ...

The compiler will only believe you if what you're saying is plausible. For example, it will not believe this...

Of course, even if the compiler accepts what you say, there will be a runtime exception if what you say turns out to be wrong.
[ August 15, 2007: Message edited by: marc weber ]
 
Maxi Malahleha
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Question about casting: Is cast like drawing a line between two Objects making either Has/ Is-a relationship.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bodix Malahleha:
... Is cast like drawing a line between two Objects making either Has/ Is-a relationship.

It's more like drawing a line between a reference and a type. A cast says that a reference points to an object that IS-A certain type. It has nothing to do with HAS-A.

For example, if you say...

Car c = new Volkswagen();

...then the variable 'c' references a type of Car. But in fact, the object is a Volkswagen (that is what was created with "new"), and a Volkswagen IS-A Car. So you can cast c's reference back to type Volkswagen with an explicit cast...

Volkswagen v = (Volkswagen)c;
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:


How much? I'll take that bet.


A tip-o-the-hat to you Peter. Glad I didn't bet!!!

 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:


For example, if you say...

Car c = new Volkswagen();

...then the variable 'c' references a type of Car. But in fact, the object is a Volkswagen (that is what was created with "new"), and a Volkswagen IS-A Car. So you can cast c's reference back to type Volkswagen with an explicit cast...

Volkswagen v = (Volkswagen)c;


Thats a very good example Marc.

This way you can make sure that the object being casted is *really* passing the "IS-A" test (for your own sake atleast to debug!). Thats how the runtime environment is really able to service you with the appropriate information.

Let me try to simplify this with a real time scenario.

Lets say, casting is something like dressing up someone to act on behalf of another. For example, you can make any one dressed up like your college principal and everyone else also believe the same. This action of making others believe is similar to compilation. - There are no issues until explicitly any changes are noticed.

But in reality, when the actual action comes to picture, like the so-called-dressed-up principal has to represent the college in a board meeting, what would happen if the projected-but-not-original guy is made appear in front of the panel? Alas!! He is not the actual principal, so eventhough he got projected as if he is a principal but at runtime or reality he can NOT do the actual things done by a principal! - This is similar to the runtime, when its time to act, you get an exception.

Hope this may help you understand better in a realistic way!
[ August 16, 2007: Message edited by: Raghavan Muthu ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:
Let me try to simplify this with a real time scenario.


You mean "real-world".

The phrase "real-time" has a very specific, and different, meaning in the world of computing; it relates to computer systems that have to guarantee to process certain data within a certain time. Examples would be process control systems.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:
...you can make any one dressed up like your college principal and everyone else also believe the same. This action of making others believe is similar to compilation...

Yes, that makes sense. But note that for the compilation to work, there would need to be some IS-A relationship between Principal and Anyone. In this case, Principal would need to be a subtype of Anyone. For example...

This will compile, because it's plausible that anyone could also be a Principal (it's a subclass). But this code will fail at runtime, because anyoneDressedAsPrincipal is not really an instance of Principal.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

You mean "real-world".



Yes, thats perfectly right.

Thanks for the correction Peter.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Yes, that makes sense. But note that for the compilation to work, there would need to be some IS-A relationship between Principal and Anyone. In this case, Principal would need to be a subtype of Anyone.



I missed to mention that real-instance-at-runtime aspect in detail. As you said in your previous car example, it has to *really* pass the IS-A test to pass even at runtime.

Thank you Marc.
[ August 16, 2007: Message edited by: Raghavan Muthu ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!