This remains my weakest area of all but I'd like to throw mythinking into the ring to see if it is correct. If not, well, I'd rather hear about it now!!!
The way I see it is that the <? extends Classname> is for specifying a type of reference or argument in a single place and it is telling you that "it will accept an object of Classname or a subclass of Classname as a type". (example:
When you are using the <T extends Classname> syntax you are specifying a "replaceable parameter" that can be plugged in throughout a class or method declaration and, any place that you put the place-holder T will take on that type. (example:
note that in the above, you are just declaring the type of the specific reference(or argument). In the lower one you are defining the type of a replaceable parameter, T, that you can plug in to the remaining declaration so that the newly declared variable, method, or whatever, gets the correct type.
This is my interpretation and if it is not right I'd consider it a favor if someone in the know would "slap me around" a little. Again this remains a pretty tough little area for me and I am just trying to see if I am grabbing it at all.
SCJP - 86% - June 11, 2009
Joined: Aug 10, 2008
Originally posted by Nabila Mohammad: From what i figured from your post was that,
Means you can add anything extending base and anything extending Number
But when you talk about
Collection<T> c, Collection<T> c2)
You can add anything , but both of them should be same..
They both should be Numbers or Integers - they cant be Number and Integer...
Am I right?
So far this is my best understanding. If anyone has a different view please share.
Joined: Jan 01, 2007
Thank you for sharing all your insights.
So based from your posts, I can assume that when you see any of these, <? extends Animal> and <T extends Animal>, they can be interpreted both as "Any subclass of Animal" or "Any class that extends Animal"... They mean basically the same idea. Am I right?
The difference lies on how they are used.
I inferred these differences from K&B (right on the last Exam watch of the Generics Chapter - page 604):
1. <? extends Animal> is ONLY for Reference Variable, Method Parameter and Method Return Type declarations.
2. <? extends Animal> CAN NEVER be used for declaring Generic Class and Generic Methods. You can't say for instance,
1. <T extends Animal> can be used for declaring Generic Class and Generic Methods ONLY.
2. "<T extends Animal>" can not be used for Reference Variable, Method Parameter and Method Return Type declarations. Given that it has already been written in the class or method declaration, the following should be applicable:
Please correct me if something is wrong. Thank you in advance.