Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException on Down Casting  RSS feed

 
Santosh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends
I have a very basic question.Just see the below code.

Now tell me one thing when i tried to compile this program it was compiled successfully.But at run time it had shown on console
Now tell me in java its perfectly allowed to downcast any class from super class,then why at runtime it is throwing this ClassCastException
waiting for reply.
thank you
[ edited to help preserve code formatting us the [code] and [/code] UBB Tags -ds ]
[ June 05, 2003: Message edited by: Dirk Schreckmann ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can cast a Sub to a Super. Since Sub extends Super, any instance of a Sub is a Super (but with extra stuff only available to a Sub).
However, supe is a Super. Since a Super is not a Sub, you can't cast an instance of a Super to a Sub.
So is your question about why this threw an exception (which it should since supe is not a Sub)? Or why the compiler doesn't complain?
hth,
bear
[ June 05, 2003: Message edited by: Bear Bibeault ]
 
Santosh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for reply.
But still now one question i have.As Object is super class of all the java classes.So we cant typecast Object with these classes as it will throw ClassCastException.
Now tell me one thing like I have one method which is very generic, takes Object as Parameter and returns object of type Object.So if I try to typecast this returned Object with my own object then it will throw exception.
So now you tell me how i am going to solve this problem and also also method should be generic so that i dont have to write different method with same code for different object return types.

Thank you
Regards
 
Richard Jensen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by santosh kumar:
But still now one question i have.As Object is super class of all the java classes.So we cant typecast Object with these classes as it will throw ClassCastException.
Now tell me one thing like I have one method which is very generic, takes Object as Parameter and returns object of type Object.So if I try to typecast this returned Object with my own object then it will throw exception.
So now you tell me how i am going to solve this problem and also also method should be generic so that i dont have to write different method with same code for different object return types.


I'm not sure I quite understand your problem. Given classes that are related by inheritance, you can safely downcast only when the object actually pointed to at the time of the downcast is of the specified type. Otherwise you get the ClassCastException.
So:

Does that help at all?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santosh,
You might enjoy the "How my Dog learned Polymorphism" story in The JavaRanch Campfire Stories. It doesn't deal explicitly with casting, but it might help get ya started with a few of these concepts.
 
Santosh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a term downcasting in java then when it is allowed.I know it is allowed in primitive data types but where else it is not.
Can you tell me ?
Thank you
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can downcast only when an object is actually the object you are downcasting to. For example:
Object a = new Button();
Button b = (Button)a;
This is a perfectly valid downcast because "a" really is a Button.
So if a method returns an Object but you know that Object is really a PotatoChip then downcasting to a PotatoChip is valid and will work.
For example:
PotatoChip pc = (PotatoChip)myArrayList.get(0);
As long as the ArrayList really contains a PotatoChip this will work.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!