May 4, 2022

Object Class in Java

Object class in Java, residing in java.lang package is the root of the Java class hierarchy. Every class in Java inherits either directly or indirectly from the Object class.

If a Java class doesn't extend any other class then it implicitly extends Object class making it a direct descendant of Object class, if a Java class extends other class then also it extends Object class as a multi-level hierarchy inheritance.

Since Object class in Java is the root of the class hierarchy, you must take note of the following three points-

  1. Object class is the parent class of all the other classes.
  2. Since it is a parent class of all the classes so reference of type Object can refer to object of any class.
  3. Every class you use or write inherits the instance methods of Object class.

Java Object class methods

Object class methods are available in every object. You may need to override these methods with code that is specific to your class.

equals() method

The equals() method compares two objects for equality and returns true if they are equal. The equals() method provided in the Object class in Java uses the identity operator (==) to determine whether two objects are equal, that will be true only if object references are equal.

In order to test whether two objects contain the same information you must override equals() method and provide the implementation that uniquely identifies an object. For example a Person class which overrides equals().

public class Person {
  public boolean equals(Object obj) {
    if (obj instanceof Person)
      return this.getId().equals((Person)obj.getId()); 
      return false;

Refer Difference Between “==” Operator And equals() Method in Java to know more about equals() method and where to use it.

hashCode() method

Default implementation of the hashCode() method in the Object class returns the object's hash code, which is the object's memory address in hexadecimal.

If you override the equals() method, you must also override the hashCode() method because of the general contract between these two methods which states; if two objects are equal, their hash code must also be equal.

finalize() method

The Object class in Java provides a callback method, finalize(), that may be invoked on an object which has no references and is about to be garbage collected.

Object class implementation of finalize() does nothing, you will have to override finalize() method to do cleanup activities.

Note that from Java 9 finalize() method is deprecated.

clone() method

clone() method is used to create a copy from an existing object. You need to invoke clone() method on an existing object for creating a clone.


For clone() method to work, your class, or one of its superclasses, must implement the Cloneable interface. Object class implementation of clone() method verifies whether the object on which clone() was invoked implements the Cloneable interface. If the object does not, the method throws a CloneNotSupportedException exception.

Refer Object Cloning in Java Using clone() Method to know more about clone method.

toString() method

The Object class toString() method returns a String representation of the object. Default implementation returns the hashCode as a hexadecimal.

public String toString() {
  return getClass().getName() + "@" + Integer.toHexString(hashCode());

Since the String representation for an object depends entirely on the object so you must override toString() method in your classes and get field values and display it, that helps with debugging.

For example, if you have a Person class with fields id, name and age then you can override toString() method and get all the field values.

public String toString() {
  StringBuffer sb = new StringBuffer();
  sb.append("Id ").append(this.getId()).append(" Name ")
  .append(this.getName()).append(" Age ").append(this.getAge());
  return sb.toString();

Which you can display using toString() method- System.out.println("Person " + person.toString());

getClass() method

The getClass() method returns a Class object. Class object in turn has methods to provide information about the class. For example, you can get class' name by invoking getSimpleName() method, its superclass by invoking getSuperclass() method.

As example- If you want to get the class name and display it.
Class c = person.getClass();
System.out.println("Class Name " + c.getSimpleName());

getClass() method is marked as final in the Object class so it can’t be overridden.

wait() method

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

There are other two variants of wait method too defined in Object class-

wait(long timeout)- Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

wait(long timeout, int nanos)- Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

All of these wait() methods in the Object class are marked as final so these methods can’t be overridden.

notify() and notifyAll() methods

notify()- Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened.

notifyAll()- Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

Both notify() and notifyAll() methods in the Object class are marked as final so these methods can’t be overridden.

Refer wait(), notify() And notifyAll() Methods in Java to know more about wait(), notify() and notifyAll() methods.

That's all for the topic Object Class in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

No comments:

Post a Comment