This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of Software Teaming: A Mob Programming, Whole-Team Approach and have Woody Zuill and Kevin Meadows on-line! See this thread for details.
In <? extends XXX>, "?" represents any type which is child of XXX. Suppose there are B, C and D child classes for base class A, then <? extends A> represents that any of the classes types B, C and D can be passed to "?" which is bounded by base class A.
Iam preparing for 1.5 certification and, if there is any worng in the above explination, please correct me.
I think one of the main questions and confusion that can occur is really understanding the difference between
<T extends XXX>
<? extends XXX>
In some ways they are the same thing, but they are used in different contexts.
When you are creating something Generified, like a template class or template interface, you generally have no idea what the actual implementation class will want to use, and you define a letter, to be overwritten later in an actual implementation class. That letter being "T" which generally stands for Type.
Now, lets say you have a class that has a method that you are not sure who will be calling it, what type it will be passing it, and you are not generifying the method or class, but just want to allow someone to call it. That is when you use the "?" style.
So let's look at the Collection Classes. Instead of using T, they use E. What they are doing is generifying the Collection Class, it is not an actual instantiated object. When you instantiate a List, you define what "E" stands for as in List<MyObject> is a List of MyObjects.
so here is an example. I am going to generify a method in an interface
in that case I cannot use a "?". It isn't allowed.
now I am actually implementing the interface, and I have
Now I can use the ?, and I cannot use T or S because I am not generifying the method I implementing a method, in which I don't know which type of List they will send to me. It can be a List of Longs or a List of Numbers. The only thing I now can't do is add to the List that is being passed to my real getSomething method. Another Topic.
Originally posted by Balaji Sampath: Hey Marc, So is there anyway we can also generalize in the class declaration .. public class MyClass implements MyInterface<String, ?>
No, because what does that mean. "?" who is ever going to define what "?" is or might be. Your Myclass is the only place it will ever be defined. But, of course, the best way to see for yourself it to try and see what happens.