0

Lets suppose class C extends B, B extends A.

For given function:

public static void add(List<? extends B> list) {
    list.add(new A()); //Doesn't compile
    list.add(new B()); //Doesn't compile
    list.add(new C()); //Doesn't compile
}

Why it's impossible to add any element ? I understand

List<? extends B> list

as a list of elements of type B or C.

For super bound:

public static void add2(List<? super B> list) {
    list.add(new A()); //doesn't compile
    list.add(new B());
    list.add(new C());
}

I understand

List<? super B> list

as a list with elements of type A or B.

I don't understand how does it work :/

user109447
  • 1,069
  • 1
  • 10
  • 20

0 Answers0