• 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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



In the above code i am getting compile time error at line 1, line 2, line 3.....

IN the method argument i am saying that accept anything that extends Number..... so line 1 and line 2 should not give error..... can you please explain me the reason why it is behaving like that?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does Object extend Number?? The answer is no so line 1 should give error. Now do you know exactly that type would be given to you at runtime? What if I pass the method Parent<Float>. Should I be allowed to set its data to Number or Integer?? It would be easier to understand if you think of a Collection instead of a custom class. Also you've not shown the code of Parent class so I'm assuming that it looks something like this
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic