Plese look at explanation extracted from K&B Study Guide for Java 5 about <? super ...> syntax :
Chapter 7, page 594
Now what you've said in this line 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."
But after trying a few Java codes, I could realize that is happening the opposite. Anything lower in the hierarchy is allowed but anything higher is not.
Please check out the following code :
As per above code you can see that with super I cannot add Dog's parent class but anything lower in the hierarchy is allowed.
After that I'm really confused because until now I think I was thinking in wrong way.
Could P L E A S E explain that.
Any comment would be H I G H L Y appreciated.
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Joined: Jan 19, 2006
After re-read the author's explanation I finally got it.
I made a mistake. I misunderstood the author's explanation.
well if I must comment then it goes like this. list<? super apple> t tells the compiler that the variable t can hold a list of type that is super to apple or apple itself but also tells it that since it can hold a collection of apple and its fathers whom we dont know permit them to add only subtypes of apple and apple itself since in all the cases they will comply but remember one thing too if it is a treeset then all the elements must be same type