Object class

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Object class in Java

The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java.

The Object class is beneficial if you want to refer any object whose type you don't know. Notice that parent class reference variable can refer the child class object, known as up casting.

Let's take an example, there is get Object () method that returns an object but it can be of any type like Employee, Student etc; we can use Object class reference to refer that object. For example:

Object obj=getObject();//we don't know what object will be returned from this method

The Object class provides some common behaviors to all the objects such as object can be compared, object can be cloned, object can be notified etc.

object class in java

Methods of Object class

The Object class provides many methods. They are as follows:

 

Method

Description

public final Class get Class()

Returns the Class object of this object. The Class can further be used to get the metadata of this class.

public into hash Code()

Returns the hash code number for this object.

public Boolean equals(Object obj)

Compares the given object to this object.

protected Object clone() throws CloneNotSupportedException

Creates and returns the exact copy (clone) of this object.

public String to String()

Returns the string representation of this object.

public final void notify()

Wakes up single thread, waiting on this object's monitor.

public final void notify All()

Wakes up all the threads, waiting on this object's monitor.

public final void wait(long timeout)throws Interrupted Exception

Causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify () or notify All() method).

public final void wait(long timeout,int nanas)throws Interrupted Exception

Causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify () or notify All () method).

public final void wait()throws Interrupted Exception

Causes the current thread to wait, until another thread notifies (invokes notify () or notify All () method).

protected void finalize()throws Throw able

Is invoked by the garbage collector before object is being garbage collected.

We will have the detailed learning of these methods in next chapters.