wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics" Watch "generics" New topic


Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
when to use <? extends Class> over <T extends Class>.i need some clarification on this topic.please explain me this

thanks in advance

thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178

As per my understanding,
<? extends Class> is used in the declaration of collections and what is there within <> determines what type of object can go inside that list.

<T extends Class> is more of a convention, where T refers to something that is not a collection. You can use such a "T" convention while creating a generic class/method, for example, wherein "T" can accept even primitives (if used as just <T>
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
<T extends Class>
It is also used to create a reference of type T, for example
<T extends Object>
T t=new Thread();
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

In this code you can add elements to the list

<T extends Number> void method(List<T> list, T obj)

but you can't do the same like this

void method(List<? extends Number> list, <?extends Number> obj)//compilation error

So basically you use ? if you don't want to use the actual type recieved in any means. Lets take another example

<T> void display(List<T> list)
T obj = list.get(0);

but you can't do the same with ?

void display(List<?> list)
? obj = list.get(0);//compilation error

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
thank you guys,

i am little bit clear now
Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55

we use <T> if want to write a generic class like generic Collection classes (introduced as part of Java 5).

class TestGenerics<T>{
T value;
TestGenerics(T o){
this.value = o;
public T getValue(){
return this.value;
public static void main(String[] args) {
TestGenerics<String> tg = new TestGenerics<String>("Test");
System.out.println(tg.getValue()); // prints Test

in above ex. we can pass any Type like 'String', 'Integer' etc.. to the class and same type we can refer while declaring instance variables and as method arguments etc..

where as '?' is allow to asign list of subtypes to super types which is actually not permitted in Generic collections.

like List<Number> lists = new ArrayList<Integer>();//not compile.

but it is psble using '?'
List<? extends Number> lists = new ArrayList<Integer>();

remember, whenever we use '?' in generics means readonly.
[ October 02, 2008: Message edited by: Venkata Saraswathi ]
Lakuma Yalamanchili

Joined: Sep 24, 2008
Posts: 26
? can only be used in reference declarations like as in variables, return types, method arguments and so on...! No where else..that's the simplest way to remember it I guess!!

I have a doubt with Generics....especially with the return type arguments:

Let's say we have a return type to a method -> List<E super Number>

Now, where all can this be stored, like when returned by the method:
A. <Any subtype of List><Any super type of Number>
B. <Any subtype of List><Number>

And with this as the return type to a method -> List<E extends Number>
A. <Any subtype of List><Any sub type of Number>
B. <Any subtype of List><Number>

I wonder if either of those options are right, because Generics says it is rigid with what is in between <> as it has to be exactly the same. So either of the options is wrong?

Please let me know which of those two in each of the case is right and also if there are any other possibilities of storing the returned value.

Thanks in advance,
[ October 02, 2008: Message edited by: Lakuma Yalamanchili ]
I agree. Here's the link: http://aspose.com/file-tools
subject: generics
Similar Threads
How do you extend a generic abstract class
Generic question
Can anyone explain what is the T,
Object obj = Boolean.class
Most confusing generic error