Object

God class in Java - every class inherits this class implicitly

  • Why?
    • By having the Object as the super class of all Java classes, without knowing the type we can pass around objects using the Object declaration.
    • Before generics was introduced, imagine the state of heterogeneous Java collections. A collection class like ArrayList allows to store any type of classes. It was made possible only by Object class hierarchy.
    • The other reason would be to bring a common blueprint for all classes and have some list of functions same among them — methods like hashCode(), clone(), toString() and methods for threading which is defined in Object class.

Methods

All method are public, final and void unless mentioned otherwise.

Access and Return TypeMethodParametersFunction
ObjectgetClass()returns class object
int (not final)hashCode()returns hashcode number of the object
boolean (not final)equals()Objectcompares the provided object with the current object
protected Object (not final)clone()creates and returns exact copy of object (throws CloneNotSupportedException)
String (not final)toString()returns String representation of the object
notify()wakes up single thread
notifyAll()wakes up all threads
wait()long timeoutwait for specified milliseconds (throws InterruptedException)
wait()long timeout, int nanosSame as above. Only increases precision.
wait()Waits until notify() or notifyAll() is invoked by another thread
protectedfinalize()Invoked by garbage collector before object is being garbage collected (throws Throwable)

Contract of hashcode() and equals() method

Equal objects must have equal hashcodes.

💡 If two objects have same hashcode, they may or may not be equal.

Always override hashCode when you override equals. Failure to do so will prevent your class from functioning properly in conjunction with all hash-based collections i.e. HashMap, HashSet etc

It's a common source of bugs.

References

hashCode And equals Methods Override - javapapers


Backlinks