This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic


Chhabra Ram

Joined: Sep 22, 2007
Posts: 2

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

Joined: Aug 03, 2008
Posts: 9453

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

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: Generics
jQuery in Action, 3rd edition