This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mechanism of equals(Object)? 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 "mechanism of equals(Object)?" Watch "mechanism of equals(Object)?" New topic
Author

mechanism of equals(Object)?

Yea Mua
Greenhorn

Joined: Jul 02, 2011
Posts: 15
Hi,

I am doing K&B mock exam today, and got stuck with this question, please let me know what you think...



Here is the caller:



Place //1 returns true which confuses me.

Since the equals() method in the code takes "Nearly" as a parameter rather than Object, so it cannot be considered as an valid overriden. So the comparison actually call the default equals(Object o) method to determine whether n1 equals n2. However, as I remember, default equals(Object o) works exactly as ==. So n1 == n2 return false ==> n1.equals(n2) should return false.
What is wrong of my understanding?

Thanks a lot!


OCPJP 6.0, OCEJWCD 6.0
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Hi Yea,

Try placing a SOP inside this equals method. Equals(Object o) and Equals(Classtype t) are valid overrides. But (Object o) is the better option

I am sorry the above statement is wrong. public boolean equals(Nearly n) is an overloaded method as stated by other ranch hands below.

What follows after this part of the question has been dealt with in this thread

http://www.coderanch.com/t/418225/java-programmer-SCJP/certification/Why-HashSet-allowing-duplicates
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Yea,

It doesn't override the equals method.Its just called the instance method of the class Nearly.The method returns true.
Without hashcode method also it returns true.Check that by comment the hashcode method.

Hope this helps.


Cheers Munees
My Blog
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 100

Your logic
So the comparison actually call the default equals(Object o) method to determine whether n1 equals n2. However, as I remember, default equals(Object o) works exactly as ==. So n1 == n2 return false ==> n1.equals(n2) should return false.

applies to the comparison that is done with == operator.
So, if you do a n1 == n2 check then your understanding above applies and it should return false.
But since you are doing n1.equals(n2), it is a direct method call to the equals() in your Nearly class. The equals() here is an overloaded method.

Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

equals in Nearly is overloaded version of equals method in object. so now Nearly class has two method with same name and different argument. you are calling the method by passing Nearly object. so equals(Nearly) gets called as it matches the exact argument.
<edit>
try passing an object argument. i.e, equals("string object");
</edit>
Yea Mua
Greenhorn

Joined: Jul 02, 2011
Posts: 15
Hi Shuba, Muneeswaran, Lokesh, Seetharaman,

Thank you for the help!

Yes, public boolean equals(Nearly n) is not an overriden method of equals(Object o), but a regular method.

 
GeeCON Prague 2014
 
subject: mechanism of equals(Object)?