Java Object Class

    Object Class

    The object class is the topmost class in Java or you can say object class is the class for all other classes. You can refer any object using the object class if you do not know the type. Here you can use the concept of upcasting to create a new object of parent class reference.

    For example-

    Object obj=getObject();

    In the above example, the getObject method will return the object of any type so we can use the object class for referring the returned object.

    There is some common feature that are provided by the Object class to all its class like comparable features, cloning and notifiable object etc.

    Object class methods-

    Below is the list of the methods that are provided by the object class.

    Method

    Description

    Public final class getClass()

    It will return the class object of the class type for this object. You can get the metadata of this class using this class class

    Public int hashCode()

    It will provide you with the object’s hashcode number.

    Public boolean equal(Object obj)

    It will allow you to compare this object to the specified object

    Protected Object clone() throws ConeNotSupportedExcpetion

    This will allow you to create and return the object’s clone

    Public String toString()

    This will allow you to return the object’s string representation

    Public final void notify()

    This will allow notifying a particular thread and waiting for this object’s attention.

    Public final void notifyAll()

    This will allow notifying all threads and waiting for this object’s attention.

    public final void wait(long timeout)throws InterruptedException

    This will allow the thread to wait for a specified millisecond until it gets a notification from another thread.

    public final void wait(long timeout,int nanos)throws InterruptedException

    This will allow the thread to wait for a few milliseconds and nanoseconds until it gets a notification from another thread.

    public final void wait()throws InterruptedException

    It allows the current thread to wait until it gets notified by another thread.

    protected void finalize()throws Throwable

    This method will get invoked by the garbage collector before collecting the object.