I have read that the following code snippet will result in a compiler error
void tryAddingToList(List<? extends SuperType> list) {
list.add(new SubType());
}
Now my question is:
Who exactly sets the restriction and how is the restriction set? Does the compiler specifically check for the add
method call in such a method implementation?
If yes, what if I implement my own List
and instead of add
, introduce a new addToList
method that does the same thing as add
, and call that in the tryAddingToList
method?
Even better, what if I implement my get
method such that it also "secretly" adds something to the list (don't ask me why anyone would do that; I'm just curious).