About Question enthuware.ocpjp.v7.2.1340 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

About Question enthuware.ocpjp.v7.2.1340 :

Post by kashyapa »

return this == obj? true : (theval%3 == 0 && ((GoodOne)obj).theval%3==0) ? true :false;
Please someone explain me this statement using if condition :) .

Thanx.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1340 :

Post by admin »

Code: Select all

boolean temp = false;
if(this == obj)
{
  temp = true;
}
else{
   if(theval%3 == 0 && ((GoodOne)obj).theval%3==0) {
      temp =  true;
   }else{
      temp = false;
   }
}
return temp;
HTH,
Paul.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1340 :

Post by codecodecode67 »

Why are equal()'s properties considered here when the question asks only about the contract between hashCode() and equals()?

The contract between them only states that, as said in the solution explanation:
If the equals() method returns true, the hashCode() of the two objects MUST be the same. The reverse is desirable but not necessary.
Meaning that, according to the equal() method's individual properties - an equals() method that always returns false will not be a valid equals() method as it will not be reflexive and further to that - we won't be able to see the equal objects. However, according to the contract between the two methods - not the two method's individual properties - the only property of the contract that needs to be true is the one in the quote above. Meaning that equals() returning false should satisfy the question...

Please let me know what you think.

Thanks

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1340 :

Post by admin »

For a valid equals-hashcode contract, the equals method has to be valid has well. By your logic, an equals method with a compilation error could also be valid option, if it satisfies the equals-hashcode contract. But obviously, that is not correct because implicit in the requirement is the fact that equals method should compile.

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests