• 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: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can create a type as follows...



...then I can use it...



...but how can I enforce that the returned type is of exactly the parameterized type, and not a subtype? i.e. In the above example, how can I enforce that an object of type java.lang.Object is returned, and not just any subtype of java.lang.Object (such as java.lang.String)?
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you can't, because if you have to return an object of type RT, every subtype SRT of RT is allowed.
An SRT is an RT.

You may only define an own return type and declare it final.


right?
[ January 17, 2005: Message edited by: Stefan Wagner ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can do that.
Suppose you don't know the type in advance.
Specifically, how could you write interface I such that that parameterized type T is exactly the type that is returned by implementations, enforced at cimpile-time?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Tony]: Sure you can do that.

I don't see how. I don't believe this is possible. Why would it be necessary? Why would it be a problem if an implementing class returned Strings rather than Objects?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Sure you can do that" - I was responding to the post that mentions a class being declared final implies that if it is used as the parameterized type, then you are guaranteed that no subclass types will be returned.

There are many reasons why you might want to guarantee the type of the object, returned by a method (rather than just being guaranteed that, whatever it is, it is assignable to the reference type returned) - the difference in this case, is that the type is unforeseen and so a compile-time enforcement is not so straight-forward.

I had a quick play around with composites of wildcards to no avail - surely it must be possible.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic