File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Downward casting 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 "Downward casting" Watch "Downward casting" New topic
Author

Downward casting

Balakrishna Thati
Greenhorn

Joined: Mar 12, 2008
Posts: 23
Hi,

i am performing the following downward casting.

I am thinking that the Child class will have all the methods of the Parent class so when a Parent object is downward casted to Child class, we should be access methods derived from the parent class.

Here is my code..

The above code altering between Block 1 and Block2, compiling normally but Block1 code throwing an exception java.lang.ClassCastException.

Why is it??
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
This will fail at runtime because you can not cast a superclass to a subclass reference type. Compiler can only verify that the two types are in the same inheritance tree, which it is. It is possible that obj1 can be a DEF object, so it compiles.
Child class do have all the method of parent class, but Parent class does not know of any method of child class thats why it can not be downcasted to child class unless the object it referred to is of Chile class. While downcasting to more specific class instanceof operator must be used.


SCJP 5.0 -- 97%
Balakrishna Thati
Greenhorn

Joined: Mar 12, 2008
Posts: 23
Hi Mumtaz,

As you said
Child class do have all the method of parent class, but Parent class does not know of any method of child class thats why it can not be downcasted to child class unless the object it referred to is of Chile class.


so based on this, the following code should compile and run properly


But its throwing the same Exception.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Not all the members are available to subclass... Think in terms of other modifiers . Hope that wil clear the doubt


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Balakrishna Thati
Greenhorn

Joined: Mar 12, 2008
Posts: 23
Hi James,

The show() method is public. So it will be available.

Then whats the issue with that. Why i am not been able to access it.

Even then The run time error is about the Class casting not with accessing a non-visible method.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Let me try this way....


IF THIS RUNS WITHOUT ANT PROBLEM , then you are saying that all humans are boys...IS that right.May not be.
Cristian Senchiu
Ranch Hand

Joined: Feb 08, 2009
Posts: 40
Balakrishna Thati wrote:

Even then The run time error is about the Class casting not with accessing a non-visible method.

Hi Balakrishna.
Let's put it this way:
---
Suppose the runtime would not throw on line 2.
You'll have afterwards an ABC instance which says "hey, I'm a valid DEF", that means it respects DEF's contracts.
If so, one could try to invoke its display() method.
But wait ... does it have a display method!?
Not at all, because, although pretending to be a DEF, is in fact only a poor ABC.
---
So the runtime throws at line 2 already, making impossible to have living ABCs pretending to be DEFs, saving us from wrongly/error prone usage of those ABCs.
manoj manny
Greenhorn

Joined: Mar 04, 2009
Posts: 4
If you are allowed to cast the base class into derived class, No matter if you are using the methods of only the base class which are there, but no one is stopping you from using derived class methods as well , So if you use the derived class's methods ..it will throw exception..thats why it is not allowed to do the casting in this manner.
Balakrishna Thati
Greenhorn

Joined: Mar 12, 2008
Posts: 23
Thanks manoj and Cristian

Even though my doubt is cleared, i am more of feeling that this situation should be escalated at compile time itself rather than at runtime.


But don't know why they postponed it till run time. Any guesses ??
 
GeeCON Prague 2014
 
subject: Downward casting