Author
Generic types
O Joseph
Ranch Hand
Joined: Jun 27, 2003
Posts: 75
hi, is it possible to use generic types for a vector that stores different objects e.g. other vectors and Color?
XX.
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted Jan 26, 2006 10:09:00
0
You would have to make the List's type a common base type. In the case of Color and Vector, it would be Object
There is no emoticon for what I am feeling!
O Joseph
Ranch Hand
Joined: Jun 27, 2003
Posts: 75
I have tried that but the compiler complains they are incompatible types
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted Jan 26, 2006 10:31:00
0
Works for me:
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
If you're referring to not having to cast to/from the Object then no. In order to use generics they must have a common type and casting to/from that type is the only thing you can avoid. I.e. You can't do Color c = vector.get(1) with a Vector<Object>. All the compiler knows is that the Vector contains Objects, you have to do the down cast yourself.
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jo Onibokun: I have tried that but the compiler complains they are incompatible types
Can you please post the code and compiler error? We can only guess what your problem is without these details. Layne
Java API Documentation
The Java Tutorial
subject: Generic types