Can't add value to the Java collection with wildcard generic type

后端 未结 4 1707
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 12:06

Why this code does not compile (Parent is an interface)?

List list = ...
Parent p = factory.get();   // returns concrete         


        
4条回答
  •  难免孤独
    2020-11-22 13:10

    This is because of "capture conversion" that happens here.

    Every time the compiler will see a wildcard type - it will replace that by a "capture" (seen in compiler errors as CAP#1), thus:

    List list
    

    will become List where CAP#1 <: Parent, where the notation <: means subtype of Parent (also Parent <: Parent).

    java-12 compiler, when you do something like below, shows this in action:

    List list = new ArrayList<>();
    list.add(new Parent());
    

    Among the error message you will see:

    .....
    CAP#1 extends Parent from capture of ? extends Parent
    .....
    

    When you retrieve something from list, you can only assign that to a Parent. If, theoretically, java language would allow to declare this CAP#1, you could assign list.get(0) to that, but that is not allowed. Because CAP#1 is a subtype of Parent, assigning a virtual CAP#1, that list produces, to a Parent (the super type) is more that OK. It's like doing:

    String s = "s";
    CharSequence s = s; // assign to the super type
    

    Now, why you can't do list.set(0, p)? Your list, remember, is of type CAP#1 and you are trying to add a Parent to a List; that is you are trying to add super type to a List of subtypes, that can't work.

提交回复
热议问题