Object class in Java

Object is a special class defined by the Java programming language. All classes are subclasses of Object class and it is the superclass for all sub classes.

  • If a class does not extend another class then Object class is by default its direct superclass.
  • A reference variable of type Object can refer to an object of any other class. This is true for arrays as well because arrays are implemented as classes in Java.
Object class: The class hierarchy in Java
Object class: The class hierarchy in Java


1. Methods defined in Object class

Object class defines a numer of essential methods. So they are available to all classes by default. Some methods are final while others can be overridden.

public final void notify()Wakes up a single thread that is waiting on this object's monitor.
public final void notifyAll()Wakes up all threads that are waiting on this object's monitor.
public final void wait()Causes the current thread to wait until another thread invokes the notify() or notifyAll() method for this object.
public final void wait(long milliseconds)Same as wait() but you can specify the time before giving up.
public final void wait(long millisecs, int nanosecs)It gives you even more control than wait(long)
public final Class getClass()Returns the runtime class of the object.
protected Object clone()Creates and returns a copy of the invoking objecct.
public boolean equals(Object ob)Determines whether one object is equal to another.
protected void finalize()Called by the garbage collector when an unused object is recycled.
public int hashCode()Returns the hash code value for the object.
public String toString()Returns a string representation of the objecct.
  • notify(), notifyAll(), wait() and getClass() are declared final. So they cannot be overridden.
  • clone(), equals(), finalize(), hashCode(), and toString() are not final. So they can be overridden depending on the need.

2. An Example

Let’s see some of the features of the Object class.