The Object class is the root class of the Java class hierarchy. Every Java class directly or indirectly inherits from the Object class.
- Belongs to the java.lang package and is automatically available in every Java program.
- Provides common methods such as toString(), equals(), hashCode(), and getClass().
- Allows objects of different classes to share a common base type.
Example: Using toString() and hashCode()
class Person{
String n;
// Constructor
public Person(String n) {
this.n = n;
}
// Override toString() for a
// custom string representation
@Override
public String toString() {
return "Person{name:'" + n + "'}";
}
public static void main(String[] args) {
Person p = new Person("Geek");
// Custom string representation
System.out.println(p.toString());
// Default hash code value
System.out.println(p.hashCode());
}
}
Output
Person{name:'Geek'}
321001045
Explanation: In the above example, we override the toString() method to provide a custom string representation of the Person class and use the hashCode() method to display the default hash code value of the object.
Object Class Methods
Object class provides multiple methods which are as follows:

1. toString() Method
toString() provides a String representation of an object and is used to convert an object to a String.
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();
// Calls overridden toString()
System.out.println(s.toString());
}
}
Output
Student{name='Vishnu', age=21}
Explanation: Overridden toString() prints a custom readable string for the object
2. hashCode() Method
hashCode() method returns the hash value of an object (not its memory address). Used heavily in hash-based collections like HashMap, HashSet, etc.Â
class Employee{
int id = 101;
@Override
public int hashCode(){
return id * 31; // Simple custom hash
}
public static void main(String[] args) {
Employee e = new Employee();
System.out.println(e.hashCode());
}
}
Output
3131
Explanation: hashCode() returns an integer value used in hashing-based collections like HashMap. If two objects are equal, they must produce the same hash code.
3. equals(Object obj) Method
The equals(Object obj) method compares the current object with another object for equality. By default, Object.equals() compares object references, but it can be overridden to define equality based on object data.
class Book {
String title;
Book(String title) { this.title = title; }
@Override public boolean equals(Object obj)
{
// Same object reference
if (this == obj) {
return true;
}
// Check for null and incompatible type
if (!(obj instanceof Book)) {
return false;
}
Book b = (Book)obj;
return this.title.equals(b.title);
}
public static void main(String[] args)
{
Book b1 = new Book("Java");
Book b2 = new Book("Java");
System.out.println(b1.equals(b2));
}
}
Output
true
Explanation: The overridden equals() method first checks whether both references point to the same object and then verifies that obj is a Book before casting. It compares the title values to determine whether the two Book objects are equal.
4. getClass() method
getClass() method returns the class object of "this" object and is used to get the actual runtime class of the object.
public class Geeks{
public static void main(String[] args)
{
Object o = new String("GeeksForGeeks");
Class c = o.getClass();
System.out.println("Class of Object o is: "
+ c.getName());
}
}
Output
Class of Object o is: java.lang.String
Explanation: The getClass() method is used to print the runtime class of the "o" object.
5. finalize() method
finalize() method is invoked by the Garbage Collector just before an object is destroyed. It runs when the object has no remaining references. You can override finalize() to release system resources and perform clean up, but its use is discouraged in modern Java.
public class Geeks {
public static void main(String[] args) {
Geeks t = new Geeks();
System.out.println(t.hashCode());
t = null;
// calling garbage collector
System.gc();
System.out.println("end");
}
@Override protected void finalize()
{
System.out.println("finalize method called");
}
}
Output
1510467688 end finalize method called
Explanation: The finalize() method is called just before the object is garbage collected.
6. clone() methodÂ
clone() method creates and returns a new object that is a copy of the current object.
class Student implements Cloneable{
int id = 1;
String name = "Vishnu";
@Override
public Object clone() throws CloneNotSupportedException{
return super.clone(); // shallow copy
}
public static void main(String[] args) throws Exception{
Student s1 = new Student();
Student s2 = (Student) s1.clone();
System.out.println(s1.name); // Vishnu
System.out.println(s2.name); // Vishnu
}
}
Output
Vishnu Vishnu
Explanation: clone() creates a copy of the current object (shallow copy by default). Class must implement Cloneable or else it throws CloneNotSupportedException.
7. Concurrency Methods: wait(), notify(), and notifyAll()
The Object class provides wait(), notify(), and notifyAll() methods for coordination between threads that use the same object's monitor.
- wait(): Causes the current thread to wait until it is notified, interrupted, or otherwise resumes according to the method variant used.
- notify(): Wakes up one thread waiting on the same object's monitor.
- notifyAll(): Wakes up all threads waiting on the same object's monitor.
These methods must be used while the current thread owns the object's monitor, typically inside a synchronized block or method.
class SharedResource {
private boolean available = false;
synchronized void waitForResource() {
try {
while (!available) {
System.out.println(Thread.currentThread().getName()+ " is waiting...");
wait();
}
System.out.println(Thread.currentThread().getName()+ " resumed.");
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
synchronized void makeResourceAvailable() {
available = true;
System.out.println("Resource is available.");
// Wakes up one waiting thread
notify();
}
synchronized void wakeAllThreads() {
// Wakes up all waiting threads
notifyAll();
}
}
public class Geeks {
public static void main(String[] args)
throws InterruptedException
{
SharedResource resource = new SharedResource();
Thread t1 = new Thread(resource::waitForResource,"Thread-1");
t1.start();
// Ensure Thread-1 starts waiting
Thread.sleep(500);
resource.makeResourceAvailable();
t1.join();
// Demonstrates notifyAll()
resource.wakeAllThreads();
}
}
Output
Thread-1 is waiting... Resource is available. Thread-1 resumed.
Explanation: wait() causes Thread-1 to wait and releases the object's monitor. The notify() method wakes one thread waiting on the same monitor. The notifyAll() method wakes all waiting threads; in this example, no threads remain waiting when it is called.