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.get(0);}
public B getFirstBottomFloorAnimal(){return bottomFloor.get(0);}
//These 3 methods compile but when I try to use it, they all only return objects
public List<Animal> getAnimals()
{
List<Animal> a = new Vector<Animal>(topFloor);
a.addAll(bottomFloor);
return a;
}
public List<A> getTopFloor(){return topFloor;}
public List<B> getBottomFloor(){return bottomFloor;}
}
I then try to do something like the following:
for(Animal a : shelter.getTopFloor()){
a.growl();
}
But I compiler error that I get the error that A is an object, not an animal. The same happens if I try to use the other methods. Any ideas why this is? Does this have to do with the List<String>
is NOT a List<Object>
idea in the Generics tutorial?
Thank you