Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downward casting

 
Balakrishna Thati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Balakrishna Thati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not all the members are available to subclass... Think in terms of other modifiers . Hope that wil clear the doubt
 
Balakrishna Thati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic