aspose file tools
The moose likes Beginning Java and the fly likes ClassCastException on Down Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "ClassCastException on Down Casting" Watch "ClassCastException on Down Casting" New topic
Author

ClassCastException on Down Casting

Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
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 ]

Santoh Kumar<br /> India<br />SCJP 1.4 <br />SCWCD 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56529
    
  14


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 ]

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
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

Joined: May 14, 2003
Posts: 67
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?


Richard
N 37 33 W 122 18
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
 
subject: ClassCastException on Down Casting
 
Similar Threads
Clarify....
Constructors.
91%
need help
overriding method question and a super question.