using extends with Java Generics
Posted
by Sandro
on Stack Overflow
See other posts from Stack Overflow
or by Sandro
Published on 2010-04-18T13:10:55Z
Indexed on
2010/04/18
13:13 UTC
Read the original article
Hit count: 303
Lets say that I have the following code:
public class Shelter<A extends Animal, B extends Animal>
{
List<A> topFloor = new Vector<A>();
List<B> bottomFloor = new Vector<B>();
public A getFirstTopFloorAnimal(){return topFloor.firstElement();}
public B getFirstBottomFloorAnimal(){return bottomFloor.firstElement();}
//This compiles but when I try to use it, it only returns objects
public List<Animal> getAnimals()
{
Vector a = new Vector(topFloor);
a.addAll(bottomFloor);
return a;
}
}
Now for somereason the following code compiles. But when I try to use getAnimals() I get a of objects instead of Animal. Any ideas why this is? Does this have to do with the List is NOT a List idea in the Generics tutorial?
Thank you.
© Stack Overflow or respective owner