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

Doubt in Downcasting

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When i compiled and ran the below code,

I got a Runtime Exception as below,

Its just an ordinary Downcasting right ?
Then why this Exception was thrown ?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pradeep rajkumar wrote:
Its just an ordinary Downcasting right ?
Then why this Exception was thrown ?



You can't cast an object to something that it is not -- so, an object referenced by a Tree referenced can only be cast to a Redwood, if and only if, it IS-A Redwood.

Henry
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BTW, what's a "ordinary" downcasting? which leads to what are the other types of downcasting?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To second Henry Wong,

You are having a tree object (super class) which you are casting to redwood. It wouldn't work that way.
A Redwood ISA tree but a Tree IS-NOT-A redwood.

if you had code like go2(new Redwood(), new Redwood());
then your cast would work. Ofcourse you won't need a cast at all in Line 10
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pradeep rajkumar wrote:Hi,
When i compiled and ran the below code,

I got a Runtime Exception as below,

Its just an ordinary Downcasting right ?
Then why this Exception was thrown ?




You cannot downcast an actual parent object to a child object. The cast will only work if the reference variable is of the parent type but the actual object being referred to is of the child type. So, the acutal object Tree cannot be cast to a child object (Redwood).
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the Tree does not comply with the IS-A relationship.
But why does this fail at runtime and not compile time?

That's the thing that I don't under stand.

Many thanks!!
 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because at compile time, there is no actual object. It is POSSIBLE that what the reference points to really is a redwood.

it is not until the objects are actually created that we know for sure.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you put in an explicit cast, you're effectively telling the compiler "I know what I'm doing - trust me, the object is going to be of this type". The compiler believes you, unless you're trying a cast that can't possibly work (like trying to cast a Tree to a Dolphin). If it might work, the compiler will let it through.

 
Nathan Brennan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fred rosenberger many thanks for the reply!

Funny, as trying to learn all the difficult stuff you sometimes miss the obvious and easy stuff!!!

Noob up a nooby!
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply IS-A fail relationship, therefore, you need an instanceof to check before the casting to avoid runtime exception and code quality.

Here is a snippet:



The code above will never execute if an "instanceof" test fail, that is Tree IS not Redwood so the cast won't happen.

Hope it help....
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic