• 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

Question regarding Generics

 
Ranch Hand
Posts: 108
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I don't understand why the code below doesn't work:




What I understand is that myMethod returns type is a class that mus be the same or superclass of something that extends CharSequence. Then... why CharSequence is not a valid Class?

Sorry but it is pretty messy for me.

Thanks in advance.
 
Alfonso Sanz
Ranch Hand
Posts: 108
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No idea about how to re-edit my post. Anyway I wanted to say: myMethod returns type is a List of class that must be the same or superclass of something that extends CharSequence.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alfonso Sanz wrote:
What I understand is that myMethod returns type is a class that mus be the same or superclass of something that extends CharSequence. Then... why CharSequence is not a valid Class?



First, what the method is returning is a generic list (wildcard), ie. List<? super E> ... and in the example call, E is the String type, so the method is returning a List<? super String>. This is definitely not something that must "be the same or superclass of something that extends CharSequence".

In the body of the method, you are returning a List<CharSequence>. This is fine, as a List of CharSequence should be assignable to the wildcard type.

What fails is the body of the main() method. The generic method is returning a wildcard -- and not a List<CharSequence> type. Arguably, while the generic method code is actually returning a List<CharSequence> instance, that information is lost to the caller. The compiler only knows it as returning the wildcard, a List<? super String> instance.  And obviously, a List<? super String> instance is not assignable to List<CharSequence> reference variable.

Henry
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 12,compiler is infering E(for myMethod) as a String so according to the declaration the return type would become List<? super String> which means on invoking myMethod you are getting a returned list having type List<? super String>(during compile time).and what are you trying on line 12 is some thing like this:
List<CharSequence> = List<? super String> --------- <1>
if it were allowed then type safety would break as here it would be so easy to put a object in List<CharSequence>.
let's see the captured types for List<? super String> they are
List<Object>,List<Serializable>,List<Comparable<String>> and List<CharSequence>
so as per the capturing if <1> is allowed then it is easy to say these:
List<CharSequence> = List<Object> havoc! type safety broken(here it is too easy to put objects in list of charsequence)
List<CharSequence> = List<Serializable> ....and so on

you may be thinking then why the compiler allow you to return list "out".because List<? super String> = List<CharSequence> is valid as the List<CharSequence> is a subclass of List<? super String>(holds the is a relationship),but the converse is not true.

for the compilation change line 12 to:

but it is not a good idea to use wild cards in return type as the users of this method have to use it every time in their code on each call(you can see the same thing in your code).

Hope it helps!

Kind regards,
Praveen.
 
Alfonso Sanz
Ranch Hand
Posts: 108
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you praveen kumaar and Henry Wong for the answer.

 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic