Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting interface to object.

 
sreenivas jeenor
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

i am working on this code.

how to achieve runtime casting for the following code


Similarly other classes like XPN, XCN implements composite.

in the Main file the code is..
-----------------------


--------------------

help me...
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried:



Also, in your Varies class what is the type of 'data' that you return from getData()?

Hunter
 
sreenivas jeenor
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Returns the data contained by this instance of Varies. Returns a GenericPrimitive unless setData() has been called.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did the example code I posted work? was there an error?

Hunter
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreenivas,
In future, while posting code, please UseCodeTags
I have edited and added them in your post. As you can see the tags make the code much more easy to read and understand.
 
sreenivas jeenor
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private Type data;

here is



its shows an error java.lang.ClassCastException: GenericComposite cannot be cast
to CWE
 
Campbell Ritchie
Sheriff
Pie
Posts: 49782
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought that requiring so many casts implies a design error somewhere.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have thought that requiring so many casts implies a design error somewhere.


To me that makes it look like the design should involve Generics in some way. Something perhaps like this:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49782
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Paul C, that looks a lot better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic