This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
List<Object> aObj = new ArrayList<Object>(); // generics are invariant
now you try to do a cast to List<Test> as in List<Test> aTestList = (List<Test>)aObj ;
here -> aObj can contains Object which is super class of Test so you will be in trouble here after if the cast is Ok hence error at compile time
List<?> aObj1 = new ArrayList<Test>(); for better understanding consider below assignment
List<? extends Object> aObj1 = new ArrayList<Test>(); //List<?> is similar to List<? extends Object>
now List<?> or List<? extends Object> points to List<Test> , mean while List<? extends Object> can also points to List<Object> i.e,
List<? extends Object> aObj1 = new ArrayList<Object>();
So compiler assume that, (List<?>)aObj this means you are casting List<Object> to List<? extends Object> which can legally point List<Test>
so allowed the following cast (List<Test>)((List<?>)aObj)
not at all dumb question
<edit>corrected typo List<T> to List<Test></edit>
Joined: May 09, 2007
Excellent explanation Mr.Seetharaman. Thanks a lot .