• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about ClassCastException (was: Doubt)

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain when class cast exception will occur with respect to down casting
[ May 14, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ClassCastException:
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi John,
Can you give an example of legal downcasting?
thanks!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Sample
{
public static void main(String[] args)
{
Object obj = new String("hello");
String str = (String) obj;
.....
}
}

Regards
Kumaran J
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically the ClassCastException arises when the JVM discovers that you have broken a promise that you have made to the compiler when you made the downcast.
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you elucidate it with an example??
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kumaran,
thanks for the reply... i got my answer.

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sravanthi pulukuri:
Can you elucidate it with an example??



Hi,

See the code to get it in better way:



Note: Compiler does not know the runtime type, because object creation
and its reference assignment is matter of runtime. Compiler only
wants that what assignment you are doing is castable. It can't see what
object the ref variable is pointing to. K&B says, here compiler relies on
us, in fact it has no other alternative except relying on us here.

But JVM see that we are trying to assign Animal object to Dog ref variable
hence ClassCastExeption. Dog is Animal for sure but Animal is not required
to be Dog only (It can be Horse, Cow, Buffalo etc)

By the way John Stone gave nice example with String and Object,
and very similar question I faced in exam too.



Thanks,
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I blatently steal Chandra's example and make it more explicit:


[ May 14, 2007: Message edited by: Barry Gaunt ]
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic