• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"T extends Animal" VS "? extends Animal"

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Steve Ng
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic