generics and subtyping


We can then plug in different things for E to get different types: for example, Collection<Animal> has methods Animal get() and void add(Animal e)

The way to think about subtype relationships between wildcard types is by thinking about what methods exist in a wildcard type, and then seeing if the proposed subtype methods are more specific than the proposed supertype.

For example, Collection<? extends Animal> has methods Animal get() but no add method at all. Collection<Dog> has a get method; it returns a Dog and a Dog is-an Animal, so this satisfies the specification of Collection<? extends Animal> so a Collection<Dog> is-a Collection<? extends Animal>.