• 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 - Why does it work?

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method

public static<T> T writeAll(Collection<? extends T> coll, Sink<? super T> snk){
T last = null;
for (T t : coll) { last = t;
snk.flush(last);
}
return last;
}
Sink<Object> s1 = null;
Collection<Object> coll = null;
Object s2 = writeAll(coll, s1);



Here Type T is of Object type. If i elaborate the method, it means Object extends Object and Object super Object. But this is ideally not true. It might be i've asked a dump question... but i am curious to know it.

Thanks
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you mean this

<? extends T>

and this

<? super T>

. I think it means - T or any subclass of T and similarly T or any superclass of T
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i am passing Object as an argument, doesn't it mean that T is representing Object.

Would you please explain me the terminology actually means.

Thanks a lot.
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If i elaborate the method, it means Object extends Object and Object super Object.

No.

Collection<? extends T>

means (as I said earlier) pass a 'T' or a subclass of 'T' as type parameter. Similarly for the 'super' also.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Generics is confusing probably because it wasn't included from the very beginning. Rather than using reification (not certain about that but I think it means there is an object associated which records the type information) Java uses erasure, which means all type information is deleted at runtime.

Rakesh Sugirtharaj is quite correct that <? super T> means T or any of its superclasses. The ? bit is pronounced "unknown" and it means that a generic type can be a subclass of another generic type, as well as their <T> being subclasses of each other. You will find a nice clear explanation about the difference between Cage<Animal> Cage<Lion> and Cage<Butterfly> in the Java� Tutorials.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completely understand that java uses erasure for generics during run time. I've also worked on generics. But when i was going through some tutorials, this question suddenly came to my mind. If any one can see the code i mentioned and based on that can reply me.... What does T represent during evaluation.

Kindly help.

Thanks

[ November 10, 2008: Message edited by: Patricia Samuel ]
[ November 10, 2008: Message edited by: Patricia Samuel ]
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It is mentioned in effective java

The use of the keyword extends is slightly misleading:
recall from Item 26 that subtype is defined so that every type is a subtype of itself,
even though it does not extend itself



Now i am pretty clear with my doubts.


Thanks to all for your kind help.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done ( ) and thank you for posting the quote. I have seen that definition in unrelated languages. If you google for ECMA 367 which is the official standard for the Eiffel Language (you can find it on the ECMA website) it includes

8.6.10
Definition: Ancestor type, of a class
The ancestor types of a type CT of base class C include
1: CT itself.
2: (Recursively) The result of applying CT's generic substitution to the ancestor types of every parent type for C.
The ancestor types of a class are the ancestor types of its current type.

That appears to be the same thing; a class is regarded as a supertype of itself (and as a subtype of itself).
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
? extends T actually means: any class X for which X IS-A T.

So that not only means class T itself and all of its subclasses, but also all implementations should T be an interface:

[ November 10, 2008: Message edited by: Rob Prime ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic