On Feb 11, 3:14 am, "Neroku" <n37...@gmail.com> wrote:
> Hello, I have a doubt about generics usage with the iterator
> interface.
> The following declarations should be 'equivalent', I mean, all the
> methods return the same type (a reference to Object):
>
> Iterator it;
> Iterator<Object> it;
>
> Well, now consider the following code:
>
> Vector<Animal> v;
> ...
> Iterator<Object> it = v.iterator();
>
> It doesn't work, since v.iterator() returns a Iterator<Animal>
> reference, which is incompatible with Iterator<Object>,
> but:
>
> Vector<Animal> v;
> ...
> Iterator it = v.iterator();
>
> It works fine, but v.iterator() returns an Iterator<Animal> reference,
> why are both references compatible??
>
> TIA
Try Iterator<? extends Object> it
|