Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Originally posted by Srinivasan thoyyeti:
<? super String> means we can only add Strings
if you add Object.
It seems something like this,
String s = new Object();
Hope you got it.
SCJP 5 94%<br /><a href="http://amit-wadhwa.blogspot.com/" target="_blank" rel="nofollow">My Blog</a>
public void AddAnimal(List<? super Dog> animals) is essentially, "Hey compiler, please accept any List with a generic type that is of type Dog, or a supertype of Dog. Nothing lower in the inheritance tree can come in , but anything higher than Dog is OK."
live
public void AddAnimal(List<? super Dog> animals) is essentially, "Hey compiler, please accept any List with a generic type that is of type Dog, or a supertype of Dog. Nothing lower in the inheritance tree can come in , but anything higher than Dog is OK."
Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
live
EXCEL IN ALL YOU DO
EXCEL IN ALL YOU DO
Originally posted by Chris Stann:
Keith, thank you for your explanation - I now have a good understanding of method type safety.
But I still don't understand why you would want to create a List this way:
List<? super Integer> l1 = new ArrayList<Object>();
Parag P Kulkarni wrote:hi,
But still its throwing compiler error. How would I print what is added?
Parag P Kulkarni wrote:hi,
I understand the explanations and thank you for that; but I am still confused about one thing....
I have a function "addString(List<? super String> myList) and inside I am adding few String to myList. But when I try to traverse through the list (using enhanced for loop "for (List s: myList)", I am getting compiler error stating following
Type mismatch: cannot convert from element type capture-of ? super String to String
I also tried to use ArrayList like below
But still its throwing compiler error. How would I print what is added?
SCJP 6.0 (88%)
Consider Paul's rocket mass heater. |