*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why am able to pass a Set (non-generic) to a method expecting Set<Number>? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why am able to pass a Set (non-generic) to a method expecting Set<Number>?" Watch "Why am able to pass a Set (non-generic) to a method expecting Set<Number>?" New topic
Author

Why am able to pass a Set (non-generic) to a method expecting Set<Number>?

Archna Singh
Greenhorn

Joined: Feb 11, 2012
Posts: 8


Its a question from Kathy Sierrs Practice book, Q 58 Practice Set 3. Why its compiled properly??? A HashSet is passed to a Set<Number>. Should it compile?

The answer mentioned is 2 0 1 0 1 false
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

backward compatibility .

think you have clients(say A and B) who are using your method *print(Set setObj)* .

and after some time you upgrade your method as print(Set<Integer> setObj) .

in this situation you should not force your client to change their code as your wish. what if the client B doesn't have sufficient money to change his code?
Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
Dear Archana,

Please use code tags while posting the code as it is not easy to understand the snippet other wise.

Arhaan
Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22
Hello Archna Singh, I happen to ask the same question a few days back. You'll find the answer in the second post of this thread:
Why it's possible to pass Non gen set to a method expecting Set<Number>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why am able to pass a Set (non-generic) to a method expecting Set<Number>?