Question: 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

Answer: 
My best guess is that you've declared `shelter` to use a raw type as follows:

```
Shelter shelter = new Shelter();

```

You really should do the following:

```
Shelter<Cat,Dog> shelter = new Shelter<Cat,Dog>();

```

Then you'd actually use the generics features.

### See also

* *Effective Java 2nd Edition*: **Item 23: Don't use raw types in new code**.

> 
> **If you use raw types, you lose all the safety and expressiveness benefits of generics.**
> 
> 
>
* [JLS 4.8 Raw Types](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.8)

> 
> The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. **It is possible that future versions of the Java programming language will disallow the use of raw types.**
> 
> 
>

---

> 
> `List<String>` is NOT a `List<Object>`
> 
> 
> 

Yes, Java generics are are neither [covariant nor contravariant](http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29). That is only an issue if you're trying to cast one generic type to another, which doesn't seem to be what you're trying to do.

Also, unlike what your subject title says, I don't think this has anything to do with `extends` to bound the type parameters in generics.
I think the core of your problem is here:

```
for(Animal a : shelter.getTopFloor()){
    a.growl();
}

```

How is `shelter` defined?