aspose file tools*
The moose likes Java in General 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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Mink Bruijninckx
Greenhorn

Joined: Aug 05, 2008
Posts: 2
Hi,

why doesn't this work:
public class MyClass {
public <T super String> void myMethod() {
}
}

The problem lies in the 'super' keyword, but I don't understand why.
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
1) super keyword in generics works only with wildcards, inside of a generic type specification, e.g.


Let's assume something like this (which is not allowed in generics in Java):

Now can you think of anything useful you could do with such return type? You would have to use Object reference to grab return from this method anyway. That's why the only keyword used with 'concrete' generic declarations (like T above) is 'extends'
2) In your example, why did you want to use generics in the first place? It only makes sense to parametrize method declaration, if the generic parameter type is part of its signature (an argument or a return type). It is useless to declare method taking no arguments and returning void as generic. There is no way the compiler could infer the 'real' type of the generic parameter at invocation of such method.
Mink Bruijninckx
Greenhorn

Joined: Aug 05, 2008
Posts: 2
Of course! Thanks!
It was just an example. You're right, it's completely useless code. :-)
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
I've just stumbled upon a SCJP mock question that proves I've been actually a bit wrong It is 100% legal to declare a parametrized method without the generic parameter in its signature. You can even do it with the main() method if you want. The following example compiles fine (except for 'unchecked cast' warning) and produces the output 'TEST'. Type T is erased to Object in the runtime.

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Dariusz Kordonski:

Let's assume something like this (which is not allowed in generics in Java):

Now can you think of anything useful you could do with such return type? You would have to use Object reference to grab return from this method anyway. That's why the only keyword used with 'concrete' generic declarations (like T above) is 'extends'


Suppose In above method I want to return object of 'Number' class (I know its an abstract , but lets suppose any class which is 'super' to Integer class ) , Then can`t I do that ?(Of course I can do that using Object as return type , but is it the only way to do it ? )

Pl clarify ,


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
I'm not sure what do you exactly mean by 'I want to return Number'? I guess you ask, if it's possible at all to tell in the method signature: 'it will return some super class of Integer' (e.g. Number). Because if you just want to return Number or its subclass, declare the method having Number as return type
Coming back to your question - I'm afraid there is no way of doing that with generics (at least I don't know of any). The reason for this is, as I understand it (and mentioned in the earlier post) that client invoking such method would have to use Object reference anyway to store the returned value. So the only profit we achieve is some kind of semantic information, which the client could use to dynamically cast the returned object to e.g. Number (using instanceof check). But that's simply not what generics are for in Java. This kind of information may be placed in JavaDoc.
I hope I understood your question correctly and my answer makes sense

P.S. I can think of an example when client would decide what kind of return type he wants, providing it as method argument and being limited to base classes of some given class. The method declaration could look like this:

But, again, it is not possible with the current implementation of generics.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ok , I got it ! just to ask if such conditions occurred and I want to return a type which is super to that of input parameter ! And as you said they are always other ways to do the same !

I think, SUN thoughts to implement it unnecessary OR they are other 'technique' to perform !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics
 
Similar Threads
Clarify....
interface
Generic Classes
Unable to run the following program
Collections.binarySearch() problem