tags:

views:

146

answers:

4

In OO parlance what is meant by overriding equals method and getHashCode ?

A: 

The Equals method compares two objects (or the object passed in as a parameter with the object on which the method is called).

The getHashCode returns a code that is unique for each different object.

These methods are implemented in every object. To override these methods you are redefining how these objects define equality.

To override a class, you create a method in the child class with the same signature (and sometimes another keyword eg 'override', depending on the language).

You can use the base method, or completely define a new method. If you call the base method, this must be the first statement inside the method.

Russell
A: 

Overriding equals allows you to define what the semantics of equality are for your object type, beyond the default of "are these the same object instance?" -- so you can define that two different objects whose content is the string "cat" are essentially identical. Hash codes are used as a fast proxy for equality (two objects that are equal must have the same hash code, though the reverse obviously does not hold) for insertion into hash-table or similar data structures.

It only makes sense for types that are essentially immutable so that comparisons done once will remain true (consider the hash table use case).

Steve Gilham
A: 

I'll try to explain. A base class for the class you are writing has already provided a Equals and GetHashCode method and has marked these so that you can provide your own implementation if needed.

You can provide your own implementation by writing a method with the same signature as the methods in the base class and mark them as being an 'override' for the methods in the base class.

This tells the compiler that whenever someone calls Equals or GetHashCode you want your implementation to be used instead of the one provided in the base class. Most languages will allow you to call the implementation of the base class in some way. This way can build logic to fall back to the base implementation or use the base implementation as a starting point of your own implementation.

Jonathan van de Veen
+1  A: 

These two methods are essential to management of objects. equals() is used to compare two objects (not necessarily of the same type, mind you) and hashCode() gives a, hopefully, unique numeric representation of the object.

In most Object-oriented languages, every class has a default implementation of these two methods, which is not good enough for your class. It is necessary to override this default implementation with meaningful code in order to make the methods useful.

Where are these methods used? Let's assume you have a hash-table of objects. Inserting a new object into the table requires a numeric value for the initial index search. This is usually extracted using the hashCode() method, which preferably gives a number that is distinct (it also helps if the hash-table size is a prime number...). Searching through the table for an item would require comparing the contained items to a given sample object. This is achieved via the equals() method, and therefore it is important this method check for fields equality of the specific class, instead of just the object address in memory.

Yuval