aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""T extends Animal" VS "? extends Animal"" Watch ""T extends Animal" VS "? extends Animal"" New topic
Author

"T extends Animal" VS "? extends Animal"

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
In K&B, p 602, what does the range ("bounds") mean here:


And you can use a form of wildcard notation in a class definition, to specify a range (called "bounds") for the type that can be used for the type parameter:



What is the difference between these two:
<T extends Animal> and <? extends Animal>
Will there be any implication if you would use one or the other?

All I can interpret is that they're the same: ANY type that extends the Animal object. Is this correct? If so, why are they called range or "bounds"?

Thank you as always.
[ August 19, 2008: Message edited by: Denise Saulon ]

SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCMJEA in progress...
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Well, i believe these are the difference:

#1

public void test(T AnyType){
}
and test(new StringBuffer()) // YES
=====================================================

public void test(<? extends Object> AnyType){// NO
}
and test(new StringBuffer())

#2

public boolean add(Collection<? extends Base> c, Collection<? extends Number> c2)
return true;
}
public boolean add2(Collection<T> c, Collection<T> c2){
return true;
}

class Base{

}
class Derived extends Base{

}

and

public class Test<T extends Base>{

public static void main(String[] args) throws InterruptedException {

List<Base> list = new ArrayList<Base>();
List<Number> list2 = new ArrayList<Number>();
Test<Base> t = new Test<Base>();
t.add(list, list2);//YES
t.add2(list, list2);//NO

}
}

I think that the point to get here is that ? is more flexible than T.

Once T = something, it can't be a different thing. But ? can be anything... In this case:

add(Collection<? extends Base> c, Collection<? extends Number> c2) == add(Anything, anything which may or may not be the first anything)

BUT:

add(Collection<T> c, Collection<T> c2) == add(Anything, anything which MUST be the first anything)

Someone corrects me if i get this wrong please

[ August 19, 2008: Message edited by: Steve Ng ]
[ August 19, 2008: Message edited by: Steve Ng ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
From what i figured from your post was that,

(Collection<? extends Base> c, Collection<? extends Number> c2)

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?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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.


------------------------
Bob
SCJP - 86% - June 11, 2009
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Originally posted by Nabila Mohammad:
From what i figured from your post was that,

(Collection<? extends Base> c, Collection<? extends Number> c2)

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.
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "T extends Animal" VS "? extends Animal"