Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mechanism of equals(Object)?

 
Yea Mua
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic