File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generic types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic types" Watch "Generic types" New topic
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
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
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
 
Consider Paul's rocket mass heater.
 
subject: Generic types
 
Similar Threads
Generic Method
Generics
Why do we get(Object key) for interface Map<K,V>?
Cast issue (when using generics)
Genrecis and Wildcard