• Post Reply Bookmark Topic Watch Topic
  • New Topic

equals  RSS feed

 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does equals(Object o) method will return true only if both objects are of same class or even it will be ok if one of them is subclass object?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the Object#equals(Object) documentation? What does that say?
There are three commonly‑used references about equals: Joshua Bloch’s Effective Java. Part of old edition available here.
Google for Angelika Langer Java equals hashCode.
From the Ranch FAQ: Odersky Spoon and Venners.
Read those three links, which will answer your question. you should be able to understand them all within two weeks
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Oracle documentation :
Returns:
true if this object is the same as the obj argument; false otherwise.


I think it does mean if subclass object is being compared with super-class object then without comparing their field value it will return false ? correct me if i am wrong.
 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't override equals method in your class or parents class, equals use Object method and compare by reference (a.equals(b) is like a==b) => if B extends A and a instance of A (and only A) and b instance of B then a.equals(b) is false.

If you want to override equals method, read the Campbell links and don't forget hashcode method ;)
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default implementation of the equals method compare the object references(Until you override the equals method in your implementation). There is nothing like super class object or sub class object.
If if two object references are referring to the same object then it will return true else it will return false. for example in below code if both are referring to the same object the output will be true and second output is false.
 
Shalini Srivastav
Ranch Hand
Posts: 86
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the old discussion that you cited has anything in relation to your question other than a passing mention of subclasses.

The straightforward answer to your question: For java.lang.Object, NO, subclasses will NOT be considered equal. For that matter, an instance of java.lang.Object will only ever be equals() to itself and NOTHING ELSE. For any other class, it depends on its implementation of equals().
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I don't think the old discussion that you cited has anything in relation to your question other than a passing mention of subclasses.

The straightforward answer to your question: For java.lang.Object, NO, subclasses will NOT be considered equal. For that matter, an instance of java.lang.Object will only ever be equals() to itself and NOTHING ELSE. For any other class, it depends on its implementation of equals().


Thanks i was looking for straightforward answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!