• 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

Generic Question

 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[b]
[/b]
Can some one make me explain why this is not compiling.The compiler is telling me that Parameter is not within Bounds......

Also not understanding then how this is working ??
[b]
[/b]

Please Help me ??

Examples would be really appreciated !!

Can someone please help me with an example of my 2 Questions ??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In this case you are declaring a new K with your method declaration. Basically the code is equivalent to this code

Now here M might be resolved to something not compatible with Number, like lets say String. But Test class can only have a type which is a Number. So you'll have to bound M to a number.


Here first of all the type declaration in the method is useless. So we'll strip it out first

Now the return type would clearly work. As for the parameter, since you are using a wildcard syntax, so compile would be able to maintain type safety here. Lets actually put some code into the class to see how

Basically you can't make changes to s variable based on its type as the type is not known. So even though ? super K seems to be breaking the bounds of K, it will still result in type safe code which is the goal of generics...
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit !!
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic