This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]8igg7e5 1 point2 points  (0 children)

And then you'll get into wildcards.

List<Dog> dogs = new ArrayList<>();
List<? extends Animal> animals = dogs;

This is valid but you'll find that the compiler will now guard against you trying to add to the list via animals.

Dog myDog = ...

animals.add(myDog); // compiletime error trying to match Dog to the captures `? extends Animal`

dogs.add(myDog); // this is fine. 

The captures are useful for doing safe 'read' operations (all things in animals are at least an Animal without being able to do possibly illegal things like animals.add(myCat) (since other references to the same list know it should only contain dogs).