• 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

generics

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when to use <? extends Class> over <T extends Class>.i need some clarification on this topic.please explain me this

thanks in advance
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Santhi,

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>
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<T extends Class>
It is also used to create a reference of type T, for example
<T extends Object>
then
T t=new Thread();
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code you can add elements to the list

<T extends Number> void method(List<T> list, T obj)
{
list.add(obj);
}

but you can't do the same like this

void method(List<? extends Number> list, <?extends Number> obj)//compilation error
{
list.add(obj);
}

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
}
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys,

i am little bit clear now
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.
--
Thanks.
[ October 02, 2008: Message edited by: Venkata Saraswathi ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
? 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,
Lakuma
[ October 02, 2008: Message edited by: Lakuma Yalamanchili ]
 
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic