• 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 about generic from example Angelika Langer FAQ

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question from
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ050

output is:

overloadedMethod(Collection<?>)


when i modify code:

output is steel:

overloadedMethod(Collection<?>)


what is rule to resolve this overload method invocation (for this example)?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sergej, it's very simple, the method call is resolved at compile time.

Here compiler doesn't know what <T> will be when this method is called at runtime. Since one of the method accepts List<Number> (or List<Integer>), the compiler cannot be sure if the List<T> this method receives will fulfill the criteria or not (i.e. whether it will be List<Integer> or List<String>). So compiler *always* will resolve to Collection<?>. It doesn't matter what you actually pass, when the above method is compiled, at that time the compiler decides it will always have to call the method which accepts Collection<?>...
reply
    Bookmark Topic Watch Topic
  • New Topic