why String is an Object but Collection<String> is not a Collection<Object> with respect to unbounded wildcards of java generics

gudii9
gudii9 used Ask the Experts™
on
why String is an Object but Collection<String> is not a Collection<Object> with respect to unbounded wildcards of java generics

Please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
Cause Java does not support contravariance for generics right now.
bounded and unbounded wildcards in generics are used to bound any Type.
 Type can be upper bounded by using <? extends T>
 where all Types must be sub-class of T or lower bounded using <? super T>
 all Types required to be the super class of T, here T represent the lower bound.
 Single <?> is called an unbounded wildcard in generic and it can represent any type,
 similar to Object in Java.
 For example  List<?> can represent any List e.g. List<String> or List<Integer>
its provides highest level of flexibility on passing method argument.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial