Please do not close this question without giving me a proper argument what I am trying to say here. Well, I have seen many answers on this subject but please allow me to put my thoughts here. Consider a simple code below:
class B extends A{}
class C extends B{}
class D extends C{}
Now lets make a list
List <? extends B> = new ArrayList<B>();
In the above code I have told compiler that there a list and it can contain B or any subclass of B.
Now the compiler knows that the list can contain B or C or D. The compiler has been specified to hold B,C or D.
Now when I try to add type of B, C or D to the list it doesnt allow.
We had already told compiler that we are dealing with B or any subclass of B.
The explanation given is that we can not be sure what the list might be containing so we can not add.
Where doea the question of any uncertainty arises? we know very well that we are dealing with B,C and D so we can read type of B,C, D and should be able to add type of B or C or D. The question of uncertainty ends right when I am actually creating a list that means either a B new ArrayList() or a C new ArrayList() or a D new ArrayList()
In short: Why they didnt design it this way below: if I declare a list like:
List<? extends B> list_dealing_wt_BCD = new ArrayList<C>();
//I should be allowed to add B,C and D.
List<? super B> list_dealing_wt_A = new ArrayList<A>();
//I should be allowed to add A.
am I missing something big here:)