File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Generics - Why does it work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics - Why does it work?" Watch "Generics - Why does it work?" New topic
Author

Generics - Why does it work?

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
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
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
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


Cheers!
RSR
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
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

Joined: Dec 16, 2007
Posts: 151
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Sep 12, 2007
Posts: 300
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

Joined: Sep 12, 2007
Posts: 300
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
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

? 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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics - Why does it work?