0

What will contain the List listOfFruits ?

public class GenericsTest {

static class Fruit {}
static class Apple extends Fruit {}

public static void main(String[] args) {
    Apple apple = new Apple();
    List<? extends Fruit> listOfFruits = new ArrayList<>();
    listOfFruits.add(apple);
}

}

I don't know why when i add apple to the list, it doesn't work , However it fits so well. ( Apple extends Fruit> .

Even if i put another Fruit.

Fruit aFruit = new Fruit();
listOfFruits.add(aFruit);

Amazing even instance of Fruit doesn't work.

The only way is to remplace 'extends' by 'super'. I really don't understand.

Kikou
  • 1,895
  • 4
  • 20
  • 32

0 Answers0