Question 6 :
which statement is true?
a)The lines 2 and 3 will cause a compile error.
b)The line 4 will cause a compile error.
c)The line 5 will cause a compile error because a cast is missing.
d)The source code will be compiled with warning(s). During the runtime a ClassCastException will be thrown in the line 5.
e)The soure code will be compiled with warning(s). No exception will be thrown during the runtime.
Answer6 : d
c) No, a cast is not necessary. In a type safe code bO can contain only an orange. But our code is not type safe, because we use the generic class Basket<E> without specifying the concrete type for the type variable E. That is why we will be warned by the compiler.
Originally posted by kishore Kumar Sangam:
Hi Dude,
The following points might be note worthy,
1.The right hand side <Type> has to be freezed.
for eg: List <Integer> x = new ArrayList<? extends Number> // Compile Time error <? extends> and <? super> cannot be used on the right hand side until unless the first level has been freezed.
i.e in your case
List<List<? extends Number>> a=new ArrayList<List<? extends Number>>();
The above code is trying to instantiate an ArrayList that holds A List of Objects that are sub-classes of Number or a Number Class. In this code snippet.
The First level is ArrayList(List) -- This Type has to be freezed at compile time.
The second Level is ArrayList(List(? extends Number))
2. ? extends - YOU CANNOT ADD ELEMNETS.
The above code is adding elements of type List at Level 1 but not ElEMNETS OF TYPE NUMBER to LEVEL 2.
3. ? super - YOU CAN ADD ELEMENTS AT YOUR RISK - COMPILER WARNINGS