Please explain me why if I use the raw type A in the method test() , the get() method on my typed list returns an Object and not a B.:
public class test
{
When you decalare your argument pa in the method test, no parameter is included, (including wildcard ). Therefore the list is held in a variable which contains a list of objects, so when you try to extract an element from the list you get a object, so you would have to cast it back to the required type, in this case B.
When using a wildcard the compliler is told not promote the list to a varibale containing an list holding Object. It is told that the contents are of the list are of 'unknown type' and is to be left alone. It is then upto the programmer to ensure that when extracting an element it is assigned to a suitable varibale, without using a cast.
"Doctor, it hurts when I do this."
Avoid raw types like the plague.
Ive been doing some digging around ang found this.
http://java.sun.com/docs/books/tutorial/extra/generics/legacy.html
Basically, erasure gets rid of (or erases) all generic type information. All the type information betweeen angle brackets is thrown out, so, for example, a parameterized type like List is converted into List. All remaining uses of type variables are replaced by the upper bound of the type variable (usually Object).
By upper bound im taking it if they mean < T extends C>, C would be the upper bound, then as only B is the type varibable for aBlist then its upper bound would be Object.
Anway hope this helps (Please dont mark me down again).
+1 for interesting test case.
Looks like erasure erases everything, so in this case, you end up with.
public List mGetBList()
And erasure of List
will result in public Object get( int )
, which, of course, cannot be assigned to B
.
If there is no strong need for raw type in method signature, use generic form that you have provided, otherwise cast the object to B
B lB = (B) pA.mGetBList().get(0);