d)Error.Because in the declaration we have specified any super type of Integer,But in for each loop we are specifying Number,We cannot restrict whenever we are using lowerbound.So accepatble thing in line 4 is for (Object no:al). =========================================================================== Number is also a super type of Integer. Then why it is not acceptable?
In a List <? super Integer> al = new ArrayList<Number>();
you can not add an Object. Would be a compiler error. You can't even add a Number. Only Integers.
You may assign al to an ArrayList<Integer>, <Number> or <Object>, but in all these lists you can add only Integers. Because al's type is unknown. Known is only that the smallest it can store is Integer. Therefore the highest you can put in must also be Integer, otherwise you may get class cast exceptions. E.g. in the case when you assign it to an ArrayList<Integer>, which is perfectly legal, you also may only store Integers in it.
Therefore lines 2 and 3 are legal. Integers can be stored properly.
Things change when you look on the types, that you can get out of it: They're of type Object, because the list is guaranteed to contain any type from Integer upward. That could be Integer, Number or Object.
To be type safe, the collection must return type Object as well. Integer or Number are too small, because there could be Objects in it. OK, not in this one, as variable al refers to an object of ArrayList<Number>, but al could easily be re-referenced to another object with type ArrayList<Object>.
Therefore the "running variable" in the for-each loop "no" must also be of type Object. Every other type - like Number in this example - would cause the compiler complaining about the programmer. Therefore answer d.
And: yes, it is complicated.
all events occur in real time
We're being followed by intergalactic spies! Quick! Take this tiny ad!