Hello everybody,
I just read the chapter on Generics of the "K&B Book" but the (good) explanations given by the authors are not enough for me to feel comfortable with Generics. So I have questions that I wan't to ask the
Java Community here:
Question 1: When we do something like:
the last line generates an error at compile-time and I understand that IS actually what we want ..but... What in the Java 1.5 List API(
http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html and in the signature of the List.add(..) method) tells me that uc.add(..) on an List<?> is not permitted?
Question 2: On an List<?> is a method call like
forbidden (since it doesn't kill the declared type-safety of sc) ?
Question 3: If I create my own "class MyClass<T>" with method1 an method2 like below. What would determinate if an "MyClass<?>" could call a specific method of "MyClass<T>" ?
By writing this question, I think the trick is that the argument on Line 4 should be of the same type T as the type declared at construction (i.e.
String) because the same T is used on "Line 1" and "Line 2", and the problem is that the type of uc is unknown? Am I right ?
Question 4: removed
Question 5: A method call of "E (List).get(int index)" done of a reference of type "List<?>" does it return an Object? And the same get method call on "List<? extends D>" does it return an object of class D ?
Question 6: Given:
Does line 1, require a cast ? At line 2, is "???" Object? Does line 2 compile ?
Thanks in advance for your help...
NB: Sorry, some answers are obvious using javac and java, but I can't use them today.
[ January 24, 2008: Message edited by: jlinho ]
[ January 24, 2008: Message edited by: jlinho ]
[ January 24, 2008: Message edited by: jlinho ]