| Author |
Generalized casting to a given class
|
Joe Simone
Greenhorn
Joined: Feb 16, 2005
Posts: 25
|
|
I have some code that looks like this ... It seems kind of silly to write the same method over and over again, each time casting the iterators next() to a different class. Note: the only thing different in each of the three methods are the lines How can I take these methods and roll them into one general method that casts to a given class? Many thanks, Joe
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
You can easily cast instances of all three concrete classes to some common abstraction - superclass or interface. Now you can pass a list of anything that implements or extends Findable. Looking at Product, Customer, and Referral, an "is a" (inheritance) relationship seems unlikely. But completely unrelated classes might all implement a common interface, maybe "HasProjectStandardIdentity" or something meaningful to you.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
If you are using Java 5.0, you can use generics to reduce the amount of code you have to type in situations like this. I'm not too familiar with this new feature, so I won't elaborate any further. Besides, you can google for more information if you want. Pre-Java 5.0 requires you to use some inheritence scheme like that mentioned above, either with a base class or an interface. Layne [ February 16, 2005: Message edited by: Layne Lund ]
|
Java API Documentation
The Java Tutorial
|
 |
Joe Simone
Greenhorn
Joined: Feb 16, 2005
Posts: 25
|
|
Awesome! Thanks Stan and Layne. I implemented an interface and the code works perfectly. I will investigate the features of 5.0 to investigate further. Thanks for your very prompt reply. Kind regards, Joe
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
|
Generics would let you dispense with the casting by creating a "List<Findable>" -- A List that held Findable objects only. But you'd still have to do the same thing with the Findable interface.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Is something similar possible with generics? I really need to find some time to play around with all the new 5.0 features myself. At the moment, all my knowledge of them is purely academic. Layne [ February 16, 2005: Message edited by: Layne Lund ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Originally posted by Layne Lund: Is something similar possible with generics? I really need to find some time to play around with all the new 5.0 features myself.
No, generics don't work that way. You could do something like this (perturbing your example as little as possible It is indeed possible to parameterize classes and methods by free type parameters like "T", but under those circumstances you either can only assume Object's methods for T, or you have to explicitly restrict T: class Foo<T implements Findable> { ... } In the body of Foo<T>, you can now use Findable's methods on T, but specific instantiations of Foo<T> would be created for concrete Findable-implementing classes. The relevant conceptual difference here is that everything you do with the parameter T is type checked in Java when the template class is itself separately compiled, while in C++ it's not looked at until the template is instantiated for a specific T.
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
So it looks like in this case, generics would only save you from making the cast, but not much else. Hmm... I think I also made another mistake. In C++, you can templatize functions (methods) and classes individiually. IIRC, generics in Java can only be applied to classes. Is that correct? Layne
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Originally posted by Layne Lund: IIRC, generics in Java can only be applied to classes. Is that correct?
Nope, actually you can have individual generic methods, too, like C++ function templates. From my little pile of learning examples: Although in this example, again, you don't gain much -- sum() could merely take two Number objects as arguments!
|
 |
Nischal Tanna
Ranch Hand
Joined: Aug 19, 2003
Posts: 182
|
|
It seems kind of silly to write the same method over and over again, each time casting the iterators next() to a different class. Note: the only thing different in each of the three methods are the lines How can I take these methods and roll them into one general method that casts to a given class? Many thanks, Joe[/qb]<hr></blockquote> Hi Joe I Think After listening to all this people u may be confused as the solutions procided by them are R&D and not practical Neways i guess u can do the following way. U can use the instanceOf operator to decide to which Class the instance belongs to. [ February 17, 2005: Message edited by: Nischal Tanna ]
|
Thnx
|
 |
 |
|
|
subject: Generalized casting to a given class
|
|
|