object class

Object Class – The Super-most Class in Java

Object class is the parent class of all the classes in the Java. It is the topmost class; every class implicitly extends this class and provides the methods of Object class. You can think of Object class as great grand father of all the classes 😉 . And there is a reason why it is implicitly extended. It helps in providing common behavior to all the classes, such as:

  1. Object can be cloned i.e. Cloning
  2. Object can be compared with similar objects i.e. Collections Support
  3. Notifying threads waiting for the lock on the object i.e. Synchronization
  4. Object removal from heap using finalize i.e. Garbage Collection

Its all up-to a programmer how he wants to use these methods in his programming. But implicitly extending Object class helps providing all these functionalities to all the objects in java.

Methods of Object Class:

  1. public final Class<?> getClass()

    It returns runtime class of the object. Its the static reference of the class, which can be used to call the static methods of the class.

  2. public int hashCode()

    This method returns a hashcode value for the object. Hashcode value is the hexadecimal integer value. It is very important in implementation of HashTables.

  3. public boolean equals(Object obj)

    It returns the boolean value for the comparison of the objects, indicating that if object1 is equal to the object2. It returns true if object1 == object2, i.e. if they point to same object. For custom implementation(means overridding) of the equals, the hashcode method should also be overridden so that the contract can be maintained between them.

  4. protected Object clone()

    It returns the copy of the object. The general intent of cloning is that

    • Cloned object is not the same object as the original object.
    • There runtime classes are same.
    • The internal values of the objects should be same.

      But these are not the absolute requirement for the method to implement. So they might not be true. The class supporting cloning has to implement Cloneable interface which is a marker interface. The clone method is present in Object class, but a class has to implement clone method itself otherwise calling clone method will result in throwing a CloneNotSupportedException.

  5. public String toString()

    This method returns string representation of the object, i.e. a textual  representation of the object. By default it returns a string consisting name of the class, followed by “@” and unsigned hexadecimal representation of the hashcode of the object. It will be equivalent to the output of following code:


    Its recommended to override toString method to return more meaningful representation of the object. That could be returning a string which shows internal values of variable of the object.

  6. public final void notify()

    It wakes up a single arbitrary thread from all the waiting thread for lock on the object. It throws IllegalMonitorStateException if the thread which calls notify() is not the owner of the object’s monitor. You can read more about threads and notify,wait and notifyAll methods at Synchronization and Locks of Java Thread.

  7. public final void notifyAll()

    It wakes up  all threads waiting on the object. The awakened thread cannot proceed until the thread which called notify or notifyAll releases the lock on the object.It also throws IllegalMonitorStateException if the thread which calls notifyAll() is not the owner of the object’s monitor.

  8. public final void wait()

    This method causes the current thread to wait until another thread invokes the notifyAll or notify method or specified amount of time is elapsed. The other overloaded wait methods take the time as parameter which helps in defining the period of time for which a thread needs to wait. It throws IllegalMonitorStateException if the thread calling wait is not the owner of the object’s monitor. It also throws InterruptedException if interrupted while waiting for a notification.

  9. protected void finalize()

    This is method is called by the Garbage Collector (GC) on the object when GC determines that there are no references to the object. Programmer needs to override it to release system resources or perform cleanup. Object class provides this method without any functionality, i.e it just returns without doing anything. Overriding class can do anything in the method from doing cleanup to reviving the object by creating reference. Any exception in finalize causes finalization to be halted and exception to be ignored.

This is all about Object Class. Isn’t it amazing that it provides so many common functionalities to all the Classes? What do you think about it? Please comment below to let us know.

Related Posts

Leave a Reply

Your email address will not be published.