This declaration:
List<? extends Fruit> stmt = new ArrayList<Apple>();
you can read as:
stmt is a List of <?> where ? must extend Fruit or ? must be Fruit
..... or in terms is-a: ? is-a Fruit
so assigment of new ArrayList<Apple> to stmt is correct, because ArrayList implements List (ArrayList
is-a List),
and
Apple extends Fruit (Apple is-a Fruit)
Similary for 'super' keyword:
List<? super Apple> stmt = new ArrayList<Fruit>();
you may read as:
stmt is a List of <?> where Apple must be superclass of ? or ? must be Apple
..... or int terms is-a: ? is-an Apple
so the declaration List<? super Apple> stmt = new ArrayList<Fruit>(); is correct, because ArrayList implements List,
and
Fruit extends Apple (Apple is-a Fruit -or Apple is superclass of Fruit)
For example this declaration:
List<? super Fruit> stmt = new ArrayList<Apple>();
is wrong and gives compiler error, because Apple doesn't extend Fruit (Fruit is not superclass of Apple -or Apple
is-not-a Fruit)