aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Downcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Downcasting" Watch "Doubt in Downcasting" New topic
Author

Doubt in Downcasting

pradeep rajkumar
Greenhorn

Joined: May 26, 2011
Posts: 10
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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39


BTW, what's a "ordinary" downcasting? which leads to what are the other types of downcasting?
Anand Athinarayanan
Greenhorn

Joined: May 20, 2011
Posts: 27
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

Joined: May 30, 2011
Posts: 7
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

Joined: Sep 28, 2011
Posts: 3
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Sep 28, 2011
Posts: 3
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

Joined: Apr 13, 2011
Posts: 206
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....


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Downcasting
 
Similar Threads
Please explain DownCast Code from K&B
Casting
Casting
Down Casting to subtype
A doubt in the type casting