DEV Community

Mohammed mhanna
Mohammed mhanna

Posted on

đź§© Understanding the Object Class in Java

If you’ve ever wondered why every class in Java seems to have methods like toString() or equals(), the answer lies in the Object class — the ultimate parent of all classes in Java.

Let’s dive deep into what makes the Object class so fundamental and how to use it effectively.


🏛 What is the Object Class?

In Java, every class you create implicitly inherits from the Object class — even if you don’t explicitly say so.

That means this:

class MyClass {
    // extends Object implicitly
}

Enter fullscreen mode Exit fullscreen mode

is the same as:

class MyClass extends Object {
}
Enter fullscreen mode Exit fullscreen mode

So, every class you create automatically gains the core methods defined in Object.


đź§° Common Methods from Object

Here are the most commonly used methods that every Java class inherits:

toString()        // Returns a string representation of the object
equals(Object o)  // Compares two objects for equality
hashCode()        // Returns an integer hash code for the object
getClass()        // Returns the runtime class of the object
clone()           // Creates and returns a copy of the object (if supported)
finalize()        // Called before an object is garbage collected (deprecated)
Enter fullscreen mode Exit fullscreen mode

These methods form the foundation of many features in Java — like collections, object comparison, and debugging.


🔍 Example: Using Object Methods

class Student {
    private String name;
    private int id;

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "Student{name='" + name + "', id=" + id + "}";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Student)) return false;
        Student other = (Student) obj;
        return this.id == other.id && this.name.equals(other.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Ali", 1);
        Student s2 = new Student("Ali", 1);

        System.out.println(s1);                   // calls toString()
        System.out.println(s1.equals(s2));        // true, because we overrode equals()
        System.out.println(s1.hashCode());        // inherited from Object (can override)
        System.out.println(s1.getClass().getName()); // Student
    }
}

Enter fullscreen mode Exit fullscreen mode

đź§  Why the Object Class Exists

The Object class provides a common type for all Java objects.

That’s why you can do things like this:

Object obj = new Student("Ali", 1);

or store different types in a single collection (before generics were introduced):

List<Object> list = new ArrayList<>();
list.add("Hello");
list.add(42);
list.add(new Student("Ali", 1));

Enter fullscreen mode Exit fullscreen mode

It’s what makes Java’s type system unified and consistent.


đź§© What Makes It Special

✅ It’s the root of the entire class hierarchy.

âś… It gives you default behavior for essential methods.

âś… It allows polymorphism and generic programming.

âś… It ensures every object in Java can be treated uniformly.


⚠️ A Note About finalize()

The finalize() method in Object was once used to clean up resources before an object is destroyed.

However, it’s now deprecated — unreliable and unnecessary.
👉 Use try-with-resources or explicit cleanup methods instead.

Learn more here: Oracle Docs on Finalize (deprecated)


đź’¬ Final Thoughts

The Object class is like the invisible backbone of Java — always there, supporting everything you build.
Understanding it helps you grasp how inheritance, equality, and type systems truly work under the hood.


❓Question for You

How often do you override methods from the Object class — like equals() and toString() — in your own projects?
Share your experience in the comments 👇

Top comments (0)