• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Questions from MasterExam on equals hashcode and is-a

 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well there was question in the masterexam where the class had 3 instance variables a,b and c and the hashcode used a and b. So i think its appropriate to use a and b in equals also and not c. Also about is-a, does is-a rely on polymorphism or is it viceversa. I think its vice-versa because if the 2 classes are not related it cannot be used in polymorphism....

Also i think run() method is not defined in Thread class but overrriden from the Runnable interface as Thread also implements runnable.
 
Sheriff
Posts: 9697
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

Nitish Bangera wrote:Well there was question in the masterexam where the class had 3 instance variables a,b and c and the hashcode used a and b. So i think its appropriate to use a and b in equals also and not c.



It actually depends on what the equals and hashCode are doing. Suppose there is a question like this



Here if the values of a, b and c for the two compared objects are a=1, b=2, c=3 and a=3, b=2, c=1 respectively, so the two objects will return true on equals comparison but the hashCodes will be different. But if my equals method is like this



In this case there will be no problem (as far as I can foresee )

Also about is-a, does is-a rely on polymorphism or is it viceversa. I think its vice-versa because if the 2 classes are not related it cannot be used in polymorphism....

Also i think run() method is not defined in Thread class but overrriden from the Runnable interface as Thread also implements runnable.



Yes you are right here...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is what i thought too. Well i just needed to verify that. Well masterexam gave out a different answer so had to crosscheck.
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make sure that you fulfill the contract for equals() and hash code methods..............

That's much more important......

in the first code the contract is not fulfilled i suppose.....
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

karthick chinnathambi wrote:make sure that you fulfill the contract for equals() and hash code methods..............

That's much more important......

in the first code the contract is not fulfilled i suppose.....


I fairly agree to that and I suppose Ankit's examples demonstrate that very well.

And yes, polymorphism to actually come in place for 2 real-world representations would depend on is-a, else it would be like writing a different story, not an adaptation.

Regards
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic