• 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

class cast exception

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

IF here it is getting classcastexception
but in the case of struts we are converting ActionForm to our form type but why not in the above ex?

Formbean f=(Formbean)form
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code you have posted, tp is referring to a TParent. A TParent may not be a TChild. In your case it is new TParent(), which is definitely not a TChild. Hence casting it to a TChild will give you the ClassCastException.

If you had coded as follows-


you wouldn't get a ClassCastException cause tp is now referring to a TChild. You can safely cast it to a TChild. But an instanceof test is always recommended.

Note that every TChild is a TParent but every TParent might not be a TChild.

I can't see your struts code, but given the way we code Action classes and forms generally, it's likely that form is indeed a Formbean. Thus I think you can cast it to a Formbean safely.


 
lakshmiNarayana Vunnam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:In the code you have posted, tp is referring to a TParent. A TParent may not be a TChild. In your case it is new TParent(), which is definitely not a TChild. Hence casting it to a TChild will give you the ClassCastException.

If you had coded as follows-


you wouldn't get a ClassCastException cause tp is now referring to a TChild. You can safely cast it to a TChild. But an instanceof test is always recommended.

Note that every TChild is a TParent but every TParent might not be a TChild.

I can't see your struts code, but given the way we code Action classes and forms generally, it's likely that form is indeed a Formbean. Thus I think you can cast it to a Formbean safely.




Everything is ok but the last point you mentioned as note:"Note that every TChild is a TParent but every TParent might not be a TChild."
could you please elaborate this....?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lakshmiNarayana Vunnam wrote:
Everything is ok but the last point you mentioned as note:"Note that every TChild is a TParent but every TParent might not be a TChild."
could you please elaborate this....?



Let's say we create another subclass of TParent called JChild:



Now if you ran the code from before:


you'd get an exception on the second line because, though JChild is a TParent, JChild is not a TChild.
 
reply
    Bookmark Topic Watch Topic
  • New Topic