This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generalized casting to a given class

 
Joe Simone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Simone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic