SrcForge

Lesson 30

Object Class

The Object class is the root of all Java classes. Every class inherits its methods including toString(), equals(), hashCode() and getClass().

Java Track

Save progress as you learn.

61 lessons

Lesson content

The Object Class is the root class of the Java class hierarchy. Every class in Java directly or indirectly inherits from the Object class. It provides methods for comparing objects, generating hash codes, converting objects to strings, cloning and managing threads.

What is the Object Class in Java?

The Object class is part of the java.lang package and serves as the superclass for all Java classes.

Key Features

  • Parent class of all Java classes.
  • Provides common methods that can be inherited or overridden.
  • Supports object comparison, cloning, string representation and thread synchronization.
  • Automatically imported into every Java program.

Definition List

  • Object Class: The root superclass of all classes in Java.
  • Superclass: A class whose properties and methods are inherited by another class.
  • Hash Code: A unique integer value generated for an object, used in collections like HashMap.
  • Method Overriding: Providing a specific implementation of a method already defined in a parent class.
  • Clone: A copy of an existing object.
  • Thread: A lightweight unit of execution within a Java program.

Common Methods of the Object Class

  • getClass() — Returns the runtime class of the object.
  • hashCode() — Returns a hash code value for the object.
  • equals(Object obj) — Compares two objects for equality.
  • clone() — Creates and returns a copy of the object.
  • toString() — Returns a string representation of the object.
  • notify() — Wakes up a single waiting thread.
  • notifyAll() — Wakes up all waiting threads.
  • wait() — Causes the current thread to wait until notified.
  • finalize() — Called by the garbage collector before object destruction.

Example 1: Using Object Class Methods

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

    public ObjectClassExample(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

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

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    public static void main(String[] args) {
        ObjectClassExample obj1 = new ObjectClassExample("Alice", 30);
        ObjectClassExample obj2 = new ObjectClassExample("Alice", 30);
        ObjectClassExample obj3 = new ObjectClassExample("Bob", 25);

        // toString()
        System.out.println("obj1: " + obj1.toString());
        System.out.println("obj2: " + obj2);

        // equals()
        System.out.println("obj1 equals obj2: " + obj1.equals(obj2));
        System.out.println("obj1 equals obj3: " + obj1.equals(obj3));

        // hashCode()
        System.out.println("obj1 hashCode: " + obj1.hashCode());
        System.out.println("obj2 hashCode: " + obj2.hashCode());
        System.out.println("obj3 hashCode: " + obj3.hashCode());
    }
}

Understanding the Program

  1. toString() — Returns a readable string like ObjectClassExample{name='Alice', age=30}.
  2. equals() — Compares object contents, not memory addresses. obj1.equals(obj2) returns true because both hold the same data.
  3. hashCode() — Generates a unique integer from object data. Equal objects always return the same hash code.

Output

obj1: ObjectClassExample{name='Alice', age=30}
obj2: ObjectClassExample{name='Alice', age=30}
obj1 equals obj2: true
obj1 equals obj3: false
obj1 hashCode: 1963861438
obj2 hashCode: 1963861438
obj3 hashCode: 2076916

Hash code values may vary between executions.

Frequently Used Object Class Methods

  • toString() — Converts an object into a readable string. Called automatically by System.out.println().
  • equals() — Checks whether two objects contain the same data. Use instead of == for content comparison.
  • hashCode() — Returns an integer representation of the object. Used by HashMap, HashSet, Hashtable.
  • getClass() — Returns runtime class info. Example: obj.getClass().getName()

Advantages of the Object Class

  • Code Reusability: Common functionality is available to all classes.
  • Consistency: Provides standard methods for comparison and representation.
  • Collection Framework Support: Works seamlessly with HashMap, HashSet and other collections.
  • Runtime Information: Allows retrieval of class metadata using getClass().

Best Practices

  • Always override hashCode() when overriding equals().
  • Override toString() to improve debugging and logging.
  • Use equals() instead of == when comparing object content.
  • Avoid relying on finalize() — deprecated in modern Java versions.

FAQ

1. What is the Object Class in Java?

The Object Class is the root superclass of all classes in Java. Every class automatically inherits from it.

2. Why do we override equals() and hashCode()?

To compare object contents correctly and ensure proper behavior in collections such as HashMap and HashSet.

3. What is the purpose of the toString() method?

The toString() method returns a readable string representation of an object, making debugging and logging easier.

Object class is the parent of all Java classes.
Every Java object inherits Object class methods.
toString() returns a readable string of the object.
equals() compares object contents, not references.
hashCode() is used by HashMap and HashSet.
Always override hashCode() with equals().
getClass() returns runtime class information.

Where Java is used

Java in real-world development

Object Class

Root superclass of all Java classes. Part of java.lang, auto-imported.

toString()

Returns a string representation of the object. Override for readable output.

equals()

Compares object contents. Use instead of == for content-based equality.

hashCode()

Returns integer from object data. Equal objects must have equal hash codes.

getClass()

Returns the runtime class of the object. Used for type checking and reflection.

Best Practice

Always override hashCode() when overriding equals() to avoid collection bugs.