• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Chhabra Ram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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?
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic