Monday, August 07, 2006

java.lang.object has two very important methods defined in it. They are -
* public boolean equals(Object obj)
* public int hashCode()

It is advisable that all the new classes you define implement both these methods.

public boolean equals - As the name indicates it is used to test equality in classes. This is done in the following steps

(i) Shallow comparison - checks to see whether both the objects refer to the same reference.
(ii) Deep comparision - to compare the relevant data structures.

Java 1.4 has the following needs -
a) reflexive - x.equals(x) should return true
b) symmentric - x.equals(y) should return true iff y.equals(x) returns true
c) transitive
d) consistent
e) for any non-null reference value x, x.equals(null) should return false.


public int hashCode() -
For two objects to be equal the hashCode should stay the same and not the vice-versa. Also, it is important to note that hashCode are needed while implementing collections such as hashMap, hashTables etc.

Delegation - An object oriented term.

In object oriented programming delegation refers to a design pattern where one object relies upon the other to provide a specified set of functionality[wikipedia].

The paper pioneered delegation was : Using prototypical objects to implement shared behavior in OOS - Henry Lieberman.

Short summary on the paper

Inheritance implements sets, delegation implements prototypes. Implementing the prototype approach to sharing knowledge in objected oriented systems is an alternative mechanism called delegation, appearing in the actor languages.