This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes casting interface to object. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting interface to object." Watch "casting interface to object." New topic
Author

casting interface to object.

sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
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

Joined: Mar 13, 2009
Posts: 492

Have you tried:



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

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
Returns the data contained by this instance of Varies. Returns a GenericPrimitive unless setData() has been called.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

did the example code I posted work? was there an error?

Hunter
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
private Type data;

here is



its shows an error java.lang.ClassCastException: GenericComposite cannot be cast
to CWE
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
I would have thought that requiring so many casts implies a design error somewhere.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

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

Joined: Oct 13, 2005
Posts: 39478
    
  28
Yes, Paul C, that looks a lot better.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: casting interface to object.