On Nov 17, 9:41 pm, dzgaga <[EMAIL PROTECTED]> wrote:
> I'm having trouble understanding why the third line below won't
> compile:
>
> Collection<? extends Number> c3;
> c3 = new Vector<Integer>(10);
> c3.add(new Integer(3));
>
> Collection c is a Collection with a Bounded Wildcard that should be
> able to hold objects of subclasses of the abstract class Number,
> correct?
>
> Then, it would seem to me, that Integer, being a subclass of Number,
> should be allowable in that Collection. Yet, the compiler says, "...
> add(capture#781 of ? extends java.lang.Number) in
> java.util.Collection<capture#781 of ? extends java.lang.Number> cannot
> be applied to (java.lang.Integer)"
>
> I have no idea what that means. Any help?
You cannot use add method when using wildcard, as add wants a known
type of element, but wildcard specifies an unknown type of element.
But you can iterate over the collection.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---