This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Problem with ClassCastException

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
I was taking the self test in SCJP study guide where this code confused me.



This code throws ClassCastException when Tree is downcast to Redwood.
But I dont understand why. To try this out I used an existing code and tried upcasting and downcasting.
I dont understand how come this one is working fine.




Regards

Sudhanshu Gupta
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to say, my friend, but you are mistaken; your example is not working fine. It will compile, yes, but at runtime, the line

NewTest n = (NewTest)t1; //throws no exception

absolutely will throw a ClassCastException. A cast can never actually change an object; it can only tell the compiler something that is true. So the first cast, from a NewTest object to class Test, is OK, because a NewTest is-a Test. But the second cast fails, since the object is really an instance of Test, and can't be converted to NewTet. There's a third thing you can do which does work, and I think that's the interesting case that you're missing:

Test t2 = new NewTest();
NewTest t3 = (NewTest) t2;

You can't make the second assignment without the cast, but with the cast, it's legal, since t2 really does hold a NewTest instance.
 
Sudhanshu Gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ernest Friedman-Hill
thank you very much for clearing my confusion.
Actually I was thinking of collections which return Object instances that can be cast to other types.
The third thing you told about clarified everything.
thank you once again.

Regards

Sudhanshu Gupta
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic