Alex Rivera | Logout

Overriding the equals method vs creating a new method

Asked 2008-08-19T17:05:26.937
21

I have always thought that the .equals() method in java should be overridden to be made specific to the class you have created. In other words to look for equivalence of two different instances rather than two references to the same instance. However I have encountered other programmers who seem to think that the default object behavior should be left alone and a new method created for testing equivalence of two objects of the same class.

What are the argument for and against overriding the equals method?

Edit
Report

1 Answer

0

The Equals method is intended to compare references. So it should not be overriden to change its behaviour.

You should create a new method to test for equivalence in different instances if you need to (or use the CompareTo method in some .NET classes)

answered 2008-08-19T17:08:39.660

Your Answer