Witscale Test Center

14.6 Overriding the equals() and hashCode() methods > 14.6.2 Overriding the equals() method


14.6.2 Overriding the equals() method

Listing 14.3 shows the Bird class overriding its equals() method. Instead of comparing object references it now compares the state of two Bird objects before declaring them equal.


 

Now the Bird class overrides the equals() method. It returns true when object passed is of type Bird and has same specie as the current Bird object. When you invoke names.contains(birdTofind) methods, the collection object goes over all the elements and compare the references with birdTofind. Now the third element in names is a Bird object with same state as the Bird Object reference by birdToFind. They are equal Bird objects as per the equals() method . Hence the Bird object will be found at index 2 in the ArrayList and hence contain() method will return true.

There are certain things you need to make sure while overriding the equals() method.

1.       Be sure that the object being compared is of the appropriate type.

The equals() method takes an object of Object type as an argument. Usually two objects should  not be equal if they are of different types. Therefore, you can start comparing the two objects by first checking the type of object passed as an argument. Moreover this type check also proves useful later. For instance, when you compare Bird object contents, you need to downcast the argument object (of type Object) from Object type to Bird as:

 

Bird bird = (Bird) o;      //casting may throw ClassCastException

                             

 Now if someone invokes the equals() method by passing an object which not of type Bird, this down cast will throw ClassCastException. But if you check the type using instanceof before doing anything else, you will remove the possibility of this exception being thrown at runtime.

 

Be careful with exam question that shows equals() method that takes argument of different type that the Object. For example, Class Bird { public boolean equals(Bird b) {} }. Such method would be overloaded version and it will not override the equals() method in Object class.

 

2.      Compare the member variables which are important for deciding that two objects are equal.

You need to decide what makes two instances equal. For instance, you need to decide which member variables to compare. For performance reasons, you should compare only the necessary variables.

Besides these broad two conventions, there are some other rules that the overridden equals() method should fulfill. Similar to the first two conventions, these rules are not enforced. It means that the code compiles even if you do not stick to these rules but your code may fail unexpectedly because the equals() method is not overridden in a way it should be. These rules form the method contract for equals() method.

 

A Java contract is a set of rules that should be followed, or rather must be followed if you want to provide a “correct” implementation as others will expect it to be.