• 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

Class Cast Error

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have added a few checks from a question in whizlabs:



Can someone explain the reasons for errors and no errors in the lines i have commented.

I have commented the lines to get the code to compile. removing the comments on where i have said 'errors', will cause the error.

Thanks

[ April 17, 2005: Message edited by: M Rama ]
[ April 17, 2005: Message edited by: M Rama ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

First of all, it is always possible to cast parent to child but in reality you cannot make parent to act like child. So while compiling it verifies for casting so it compiles but when it is run it gives exception.
You think parent child relation in real so child can never take parents palce. So errow at line 1 and 2


Animal
/////////Mammal
///////////////Dog
///////////////Swampthing
///////////////Cat
///////////////Raccoon

Basically above it shows the tree of what you implemented. So class at same level Dog, Swampthing, Cat and Raccoon cannot be casted to each other.
Also, the class which implements interface can be assigned to reference of interface.

Dog d = (Dog) wawa; // will give exception error for above reason as wawa is new Cat()
Dog d1 = ( Dog ) new Cat(); // will give exception error for above reason

Thanks
[ April 17, 2005: Message edited by: Kalyani Marathe ]
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kalyani.

But, perhaps the code which I pasted was wrong as you pointed out.

Dog d = (Dog) wawa; //does give a run time error.

It kinda seems to make sense. Let me think it over some more.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mammal m = (Mammal) wawa;

doesn't give out any exception error. Why is this so?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think wawa keep reference to Cat object
and cat extends Mammal
[ April 19, 2005: Message edited by: skywalker : ]
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So class at same level Dog, Swampthing, Cat and Raccoon cannot be casted to each other

when the class at same level are casted i guess we will get the compilation error. incompatible type error. i doubt what run time exception will it get.
if i am wrong can u mention me the name of the run time exception..


by the way is the runtime error and run time excetption all the same...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

As far as i know, type casting can be done only on primitive and reference that are related.

At compile time, compiler will check whether you are assigning a superclass type to one of its subclass type. If yes, an error will be thrown. By using the cast operator, i guess this step is bypass.

Then at run-time, the object referenced by the variable will be checked against the type in the cast operator. If they don't have a "is-a" relationship, an ClassCastException will be thrown. An object can only be cast to itself or its superclasses.

Thats explain why line 1, 2 and 4 have runtime exceptions. wawa is a superclass type variable and its referenceing its subclass, Cat. Clearly, Cat is not a SwarmThing or Racoon or Dog.

For line 5, i guess because the cast operator is an unary operator and it has higher percedence over the new keyword, the statement will try to cast the keyword New. This i am not so sure, is it a syntax error at compile time?

Cheers!
 
reply
    Bookmark Topic Watch Topic
  • New Topic