• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

generic method confusing

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello everyone.

I couldn't understand why the code below does not compile.
I think I didn't get the right way to evalutate a generic method declaration.
Behind this doubt, could anyone indicate some kind of material, book or link that can help me to improve knowledge about generics?


From masterexam.



Thanks
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you specified <? super T>, which means T or any super class of T. So if T extended BaseClass, Aother class, call it S could also extend BaseClass. But if the compiler allowed it you would be able to put an S instance into your list. But an S is not a T.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Generics Super And Extends
See Generics FAQ
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading the Java Generics and Collections from O'reilly, I could understand what is going on at this code.

I'd like to post this reply to document what I understood so it can help others with the same kind of doubt.

Considering the code bellow, Let's try to use another example.


This is another example that leads me to understand it.


The code above, shows an unsafe operation and illegal because, if it was legal, it will allow to pass a List<Object> as an argument.
Allowing this, it could contains Strings that won't be a valid operand when calculating 'd' variable. It won't be able to sum ("four" + 10.12).
So, I believe that this is why a list declared with <? super Number> cannot be iterable and must be used only when adding elements into this.

The Java Generics and Collections from O'reilly, calls this principle as "the get and put principle".

Regards
 
Did you just should on me? You should read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic