I\'m struggling to get my head around this and was wondering if someone could explain the reasons for this.
I have three classes:
class Angel {}
clas
Your intuitive logic says "a List super Person>
is a list of things that are a Person
or a supertype of Person
, so naturally I can add an Angel
into it". That interpretation is wrong.
The declaration List super Person> list
guarantees that list
will be of such a type that allows anything that is a Person
to be added to the list. Since Angel
is not a Person
, this is naturally not allowed by the compiler. Consider calling your method with insertElements(new ArrayList
. Would it be okay to add an Angel
into such a list? Definitely not.
The best way to reason about it is that List super Person>
is no definite type: it is a pattern describing a range of types that are allowed as an argument. Look at List
as not a subtype of List super Person>
, but a type that matches this pattern. The operations allowed on List super Person>
are those that are allowed on any matching type.