Understanding the Object Class in Java

Vicksheet Shanbhag
2 min readMar 20, 2024

--

Photo by Educba on Google

In Java, every class is implicitly a subclass of the Object class. The Object class is the root of the class hierarchy, and it defines the behavior that is common to all objects.

Even if you don’t explicitly extend the Object class, your class automatically inherits its methods and fields. This means that every object in Java has access to the methods and fields defined in the Object class.

Methods of the Object Class

The Object class provides several useful methods that you can override or use directly in your own classes. Here are some of the commonly used methods:

toString()

The toString() method returns a string representation of the object. By default, it returns a string that contains the class name and the hashcode value of the object. However, you can override this method to provide a more meaningful string representation of your object.

public class Person {
private String name;
private int age;

// Constructor

@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

equals()

The equals() method compares the current object with the specified object for equality. By default, it compares the references (memory addresses) of the objects. If you want to compare the actual content of the objects, you need to override this method.

public class Person {
private String name;
private int age;

// Constructor

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Person other = (Person) obj;
return age == other.age && Objects.equals(name, other.name);
}
}

hashCode()

The hashCode() method returns a hash code value for the object. This method is related to the equals() method, as objects that are equal should have the same hash code. If you override the equals() method, you should also override the hashCode() method.

public class Person {
private String name;
private int age;

// Constructor

@Override
public int hashCode() {
return Objects.hash(name, age);
}
}

clone()

The clone() method creates and returns a copy of the object. By default, it creates a shallow copy, which means that only the object itself is copied, and not the objects referenced by its fields. If you want to create a deep copy, you need to override this method.

finalize()

The finalize() method is called by the garbage collector before the object is reclaimed by the JVM. This method can be overridden to perform any necessary cleanup operations before the object is destroyed.

Conclusion

The Object class provides a set of fundamental methods that are common to all objects in Java. By understanding these methods and how to override them, you can create well-designed and efficient classes that meet your application's requirements.

--

--

Vicksheet Shanbhag
Vicksheet Shanbhag

Written by Vicksheet Shanbhag

I am a Software Developer and currently work as a Full Stack Developer. I like to research about new technologies and share any knowledge or tips that can help.

No responses yet