If you have ? extends NaturalNumber ? extends NaturalNumber , this parameter may be some other subclass of NaturalNumber that is in no way associated with EvenNumber . For instance,
Collection<? extends NaturalNumber> c = new ArrayList<OtherNaturalNumber>();
valid if OtherNaturalNumber continues to NaturalNumber .
Therefore, you cannot add an instance of EvenNumber to the list. You can simply use this declaration:
Collection<NaturalNumber> c = new ArrayList<>();
which allows you to add any instance of NaturalNumber (including EvenNumber ).
In another note, you'd probably want to make these nested classes static (or not at all).
source share