• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

mechanism of equals(Object)?

 
Yea Mua
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

https://coderanch.com/t/418225/java-programmer-SCJP/certification/Why-HashSet-allowing-duplicates
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic