• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generics

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course! Thanks!
It was just an example. You're right, it's completely useless code. :-)
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ,
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic