The Object class is the root of the Java class hierarchy and belongs to the java.lang package. Every Java class, except Object itself, directly or indirectly inherits from it.
- Provides common methods for object comparison, representation, copying, and thread coordination.
- Allows objects of different classes to be accessed through a common reference type.
Example: Using Object class methods to retrieve object information.
class Person{
String name = "Geek";
@Override
public String toString(){
return "Person{name='" + name + "'}";
}
public static void main(String[] args){
Person p = new Person();
// Display object representation
System.out.println(p.toString());
// Display runtime class name
System.out.println(p.getClass().getName());
}
}
Output
Person{name='Geek'}
Person
Explanation:
- The Person class implicitly inherits from the Object class.
- The overridden toString() method provides a readable representation of the object.
- The getClass() method returns the runtime class of the object.
Object Class Declaration
The Object class is declared in the java.lang package and does not need to be explicitly imported.
public class Object
When a class does not explicitly extend another class, it implicitly extends Object.
class Student{
// Implicitly extends Object
}
The above declaration is equivalent to:
class Student extends Object {}
Object Class Methods
The Object class provides methods for accessing object information, defining equality, creating copies, and coordinating threads.

1. toString() Method
The toString() method returns a string representation of an object. Its default implementation contains the class name followed by @ and the hexadecimal representation of its hash code.
class Student{
String name = "Vishnu";
int age = 21;
@Override
public String toString(){
return "Student{name='" + name + "', age=" + age + "}";
}
public static void main(String[] args){
Student s = new Student();
System.out.println(s);
}
}
Output
Student{name='Vishnu', age=21}
Explanation: Overriding toString() provides a readable representation of the Student object. The println() method automatically invokes it for a non-null object.
2. equals() and hashCode() Methods
The equals() and hashCode() methods work together to define object equality and support hash-based collections.
- equals() checks object identity by default but can be overridden to compare object data.
- hashCode() returns an integer hash value, not necessarily the object's memory address.
- Equal objects must have identical hash codes, but objects with identical hash codes are not necessarily equal.
import java.util.Objects;
class Book{
private final String title;
Book(String title){
this.title = title;
}
@Override
public boolean equals(Object obj){
if (this == obj){
return true;
}
if (obj == null || getClass() != obj.getClass()){
return false;
}
Book b = (Book) obj;
return Objects.equals(title, b.title);
}
@Override
public int hashCode(){
return Objects.hashCode(title);
}
public static void main(String[] args){
Book b1 = new Book("Java");
Book b2 = new Book("Java");
System.out.println("Equal: " + b1.equals(b2));
System.out.println("Hash Code 1: " + b1.hashCode());
System.out.println("Hash Code 2: " + b2.hashCode());
}
}
Output
Equal: true Hash Code 1: 2301506 Hash Code 2: 2301506
Explanation: The overridden equals() method compares the book titles. The hashCode() method generates a hash code based on the same field, ensuring that equal Book objects produce identical hash codes.3. equals(Object obj) Method
3. getClass() Method
The getClass() method returns a Class object representing the runtime class of an object.
class Geeks{
public static void main(String[] args){
Object obj = "GeeksforGeeks";
Class<?> c = obj.getClass();
System.out.println("Runtime Class: " + c.getName());
}
}
Output
Runtime Class: java.lang.String
Explanation: Although obj is declared as an Object reference, it refers to a String object. Therefore, getClass() returns the String class.
4. clone() Method
The clone() method creates a copy of an object. The default Object implementation performs a shallow copy, where primitive values and object references are copied.
4. getClass() method
getClass() method creates a copy of an object. The default implementation provided by the Object class performs a shallow copy of its fields.
- Copies primitive values and object references rather than recursively copying referenced objects.
- Requires the object's class to implement the Cloneable interface when using Object.clone().
class Student implements Cloneable{
int id = 1;
String name = "Vishnu";
@Override
public Student clone() throws CloneNotSupportedException{
return (Student) super.clone();
}
public static void main(String[] args)
throws CloneNotSupportedException{
Student s1 = new Student();
// Create a copy of the object
Student s2 = s1.clone();
System.out.println("Original: " + s1.name);
System.out.println("Cloned: " + s2.name);
// Compare object references
System.out.println("Same Object: " + (s1 == s2));
}
}
Output
Original: Vishnu Cloned: Vishnu Same Object: false
Explanation:
- Cloneable indicates that the class supports cloning through Object.clone().
- super.clone() creates a shallow copy of the Student object.
- Both objects contain the same field values but have different identities.
Note: The Cloneable interface is a marker interface and does not declare a clone() method. The default Object.clone() implementation throws CloneNotSupportedException if the object's class does not implement Cloneable.
5. wait(), notify(), and notifyAll() Methods
These methods support inter-thread communication between threads sharing the same object's monitor.
Method | Description |
|---|---|
Suspends the current thread and releases the object's monitor while waiting. | |
Wakes one thread waiting on the object's monitor. | |
Wakes all threads waiting on the object's monitor. |
Example: Coordinating two threads using wait() and notifyAll().
class Geeks{
private static final Object lock = new Object();
private static boolean ready = false;
public static void main(String[] args)
throws InterruptedException{
Thread worker = new Thread(() ->{
synchronized (lock){
try{
// Wait until the condition becomes true
while (!ready){
lock.wait();
}
System.out.println("Worker thread resumed");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
worker.start();
synchronized (lock){
// Update the condition
ready = true;
System.out.println("Main thread sends notification");
// Notify waiting threads
lock.notifyAll();
}
worker.join();
}
}
Output
Main thread sends notification Worker thread resumed
Explanation:
- The lock object provides a shared monitor for both threads.
- If ready is false, the worker thread calls wait() and releases the monitor.
- The main thread updates ready and calls notifyAll().
- The worker thread proceeds after acquiring the monitor and confirming that ready is true.
6. finalize() Method (Deprecated)
The finalize() method was historically used to perform cleanup before an unreachable object was reclaimed by the garbage collector.
- Its execution and timing are not guaranteed.
- It was deprecated in Java 9 and deprecated for removal in Java 18.
Important Points About Object Class
- The Object class is the root of the Java class hierarchy.
- An Object reference can hold a reference to any Java class instance or array.
- Methods such as toString(), equals(), and hashCode() can be overridden to provide class-specific behavior.
- Methods such as getClass(), wait(), notify(), and notifyAll() are final and cannot be overridden.