• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting to an unknown class  RSS feed

 
Tim Oister
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!
I have a question regarding a flexible-type List class that I've created. Essentially, once you add an object to the List, it only accepts objects of that type. However, if I want to retrieve an object from the List, it returns an Object reference. Is there a way to cast the Object to the class that is stored in the list, so that methods of that class can be called. Here is the code for the class:

The print() method isn't critical to the class, but helps in testing. What I'd like to do is add a get() method that returns an Object, but in the calling code, I'd like to be able to cast to the correct class.
Is what I'd like to do possible or should I scrap FlexibleList?
Any help would be greatly appreciated!
Thanks in advance!
Tim
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds more like an InfexibleList to me. That is, it's precisely like a List but less flexible.
I don't think what you're trying to do is really possible...certainly not in a way that would be useful.
What I often useful is creating collections classes, which are typesafe wrappers of the standard Collection classes. For instance,

This can me used to create all sorts of typesafe collection classes.
hope this helps,
Tom
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to go out on a limb here and say that it can't be done. Java is statically typed, which means that at compile time all variables must resolve to a declared class. The problem would be where you use the get method and try to do the cast. You could make an accessor method on your list that lets you get the dynamically determined type, but at compile time, that Class variable has no type, so you'd get a compile error. The best you could do is use reflection to try to determine at runtime what class you are dealing with and what methods you can call.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim
I agree with Joe. We can't do what you are trying to do BUT I have some idea.
Okay, from your description it seems we have to know the method names for sure to call. So, that implies "methods are known". It means we can create an interface which is having that method (or set of methods for that matter) and make our objects which we are inserting into that Flexible list. Now, when we do get on that list and have some java.lang.Object sure it will be of the Interface type we created. So, we can cast the object to that interface type and then call the method(s) we want...
Does this help?
Regards
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
in my above post I have messed up sentence,
"and make our objects which we are inserting into that Flexible list"
it should be,
"and make our objects, which we are inserting into the Flexible list, IMPLEMENT THE INTERFACE"
Sorry guys. I seem to out of mind today....
Regards
Maulin
 
Tim Oister
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all! As Don mentioned, it turns out to be a somewhat InflexibleList. Although I do like Maulin's suggestion for a work-around.
Again thanks for all of your insight! FlexibleList may have been saved from the scrap heap.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.5 has generics to handle this kind of thing. Can you wait that long?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!