Every developer has to write some concrete class, using which certain business logic is achieved. Since every class is a child of ‘Object’ class, it is always advisable to override equals, hasCode and toString methods. When overriding these methods, add @override annotation to indicate the java compiler, that this method overrides the corresponding method in super class.
Methods signature : public boolean equals(Object o) – equals method checks whether the passed object is equal to the method on which equals method is invoked. The default implementation in Object class compares object passed in argument and the Object on which equals method is invoked using == operator. If they are equal, it returns true. Following approach has to be used while overriding equals method.
- Check if the object passed in argument to equals method is not-null. If it is null, return false.
- Check if the object passed in argument and the object on which equals is invoked, are of same class using instanceof operator. If they don’t belong to same class, return false. Objects with no common ancestor cannot be considered equal.
- Cast the object passed in argument to the class of object on which equals method is invoked. This is guaranteed to succeed, as instance check was done before this step. After casting, check the equality of important primitive variables using == operator. If the class has reference variables to other objects, invoke equals methods on the object recursively to which the reference variable refers. Returns true, if all these conditions return true.
Check whether the your equals methods adheres to conditions specified in java doc.
Methods signature : public int hashCode(Object o) – hashCode method returns an integer corresponding to the object. Default implementation of hashCode in Object converts object’s internal address to integer. When equals method is overridden, it is also important to override hashCode method. This is required to satisfy “If two objects are equal by overridden equals method, then their hash codes has to be equal”. It is not required that the hash codes has to be different for objects which are not equal.
Using HashCode builder provided by Apache commons relieves us from the internal details for building good distributed hash code. You can create new default HashCodeBuilder with hard-coded values or with chosen random values. Using add(*) methods for all primitives, the primitive values can be used to build hash code. Composite object can be added to hash code builder using add(Object o) methods. If the class is inherited from other class, add super class’s hash code using appendSuper method.
Reference : HashCode Builder java doc
Methods signature : public String toString(Object o) – toString method returns the string representation of object. The returned string has to be human-readable and gives the concise information about the state of the object. The default implementation of object class print class name followed by @ sign followed by hexadecimal representation of hash code of corresponding object. Using string concatenation is a bad idea for implementing toString as it creates unnecessary objects. StringBuilder can also be used to implement toString().
Again, using ToStringBuilder provided by Apache Commons relieves from building String by ourselves. You can create default ToStringBuillder by passing current instance which can be used to build string representation. The style in which the information to be printed can also be changed by providing overridden constructor. Optionally, StringBuffer can also passed to be used used for building string. All the primitives attributes are added using add<primitive type>(“name of field”, <value of field>) method. The attributes in super class can also be added using addSuper method. The composite object can be added using addToString method.
Reference : ToStringBuilder java doc