• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

ClassCastException on Down Casting

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67504
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic