• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Downcasting

 
pradeep rajkumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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 ?
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW, what's a "ordinary" downcasting? which leads to what are the other types of downcasting?
 
Anand Athinarayanan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
kawshik java
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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).
 
Nathan Brennan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic