By overriding the "public boolean equals(Object)" method.
How you do this is all up to you, but you should a) read the API of Object for some rules, and b) also override hashCode().
Now some checks you always want to perform: - if the passed object is the same object, return true - this is for speed mostly - if the passed object is null, return false - if the passed object is of an incompatible class, return false
In code:
Instead of "getClass() != o.getClass()" you also often see code like "!getClass().isInstance(o)", or the more hardcoded version "!(o instanceof MyClass)".
While the latter two are perfect for final classes, they may lead to violation of one of the API rules for equals, if a subclass defines its own equals method using the same principle. If that's the case, then equals is no longer symmetric, since an instance of the subclass can be equal to an instance of your own class, but vice versa they will be unequal.
Post by:autobot
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, that will fit in a stocking