Originally posted by rathi ji:
we are using Collection<?> it means it can take any object
No. Something with type Collection<?> is a collection of
some type, but we don't know which. It could, for all we know, be a Collection<java.util.Date>.
Also, you can't create an object with runtime type Collection<?>. When we create a collection, we must pick some type for the elements, and not just say "don't know".
With generics, Collection<Object> is not a superclass of, or more general than, Collection<String>. Neither type of object can be type converted to the other, they are both too specific.
Instead we generalize by weakening the type argument. The most generic collection is Collection<?> (or, equivalent, Collection<? extends Object> ). There is no objects with this type, but there are objects that are comatible with it (a collection of any chosen type).
We can then make a less generic type, like Collection<? extends String>. Any collection of any type that matches this type, will also match the previous one. Again there is no objects of this type.
More precise again is Collection<String>, of which you can make objects, because the type is known exactly. That means that we have a hierarchy of collection types with exact types at the leaves, and more general types in the nodes, and we can only make instances of the exact types.
Regards
/L
Edit - formatting
[ March 11, 2005: Message edited by: Lasse Reichstein Nielsen ]