VOOZH about

URL: https://www.javacodegeeks.com/javas-object-class-explained.html

⇱ Java’s Object Class Explained - Java Code Geeks


In Java, the Object class is the root class of the entire class hierarchy. Every class in Java directly or indirectly inherits from the Object class. This makes it one of the most fundamental concepts in Java’s object-oriented design. Whether you create a simple class, a complex framework component, or work with built-in classes like String, Integer, or ArrayList, all of them ultimately extend the Object class. Let us delve into understanding the Java Object Class and its role in building the foundation of all Java objects.

1. What is Object Class?

The Object class is defined in the java.lang package and is automatically imported into every Java program. It provides common methods that all Java objects can use.

1.1 Why is the Object Class Important?

  • It provides a common superclass for all Java classes.
  • It enables runtime polymorphism.
  • It allows generic handling of objects using Object references.
  • Core methods like comparison, cloning, and thread synchronization come from this class.

1.2 Key Methods of the Object Class

  • toString() – Returns string representation of an object
  • equals(Object obj) – Compares objects for logical equality
  • hashCode() – Returns hash value of the object
  • getClass() – Returns runtime class information
  • clone() – Creates a copy of the object
  • finalize() – Called before garbage collection (deprecated)
  • wait(), notify(), notifyAll() – Used for thread communication

1.3 Exploring Commonly Used Methods

  • toString() – Used for object representation in logs and debugging.
  • equals() – Used for content comparison instead of reference comparison.
  • hashCode() – Works with hashing-based collections like HashMap and HashSet.
  • getClass() – Helpful in reflection and runtime inspection.

2. Code Example

The following example demonstrates how important methods from the Object class are overridden and used.

// ObjectClassDemo.java
class Student {
 private int id;
 private String name;

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

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

 // Overriding equals() method
 @Override
 public boolean equals(Object obj) {
 if (this == obj) return true; // Same reference
 if (obj == null || getClass() != obj.getClass()) return false;

 Student student = (Student) obj;
 return id == student.id && name.equals(student.name);
 }

 // Overriding hashCode() method
 @Override
 public int hashCode() {
 return id * 31 + name.hashCode();
 }
}

public class ObjectClassDemo {

 public static void main(String[] args) {

 Student s1 = new Student(101, "Rahul");
 Student s2 = new Student(101, "Rahul");
 Student s3 = new Student(102, "Amit");

 // Using toString()
 System.out.println("s1: " + s1);

 // Using equals()
 System.out.println("s1 equals s2: " + s1.equals(s2));
 System.out.println("s1 equals s3: " + s1.equals(s3));

 // Using hashCode()
 System.out.println("s1 hashCode: " + s1.hashCode());
 System.out.println("s2 hashCode: " + s2.hashCode());
 System.out.println("s3 hashCode: " + s3.hashCode());

 // Using getClass()
 System.out.println("Runtime class of s1: " + s1.getClass().getName());
 }
}

2.1 Code Explanation

The Student class defines two private fields, id and name, and initializes them using a constructor, demonstrating encapsulation and object creation. The toString() method is overridden to return a readable string representation of the object, which is automatically used when the object is printed. The equals() method is overridden to perform logical comparison by first checking reference equality, then validating class type using getClass(), and finally comparing the actual field values, ensuring content-based equality instead of memory reference comparison. The hashCode() method is overridden to generate a consistent hash value using both id and name, which is essential for correct behavior in hash-based collections. In the ObjectClassDemo class, three Student objects are created, where s1 and s2 have identical values and s3 has different values; the program demonstrates toString() by printing s1, validates logical equality using equals(), confirms consistent hashing using hashCode(), and finally retrieves runtime class information using getClass(), thus showcasing the practical use of key methods inherited from the Object class.

2.2 Code Output

s1: Student{id=101, name='Rahul'}
s1 equals s2: true
s1 equals s3: false
s1 hashCode: 109699471
s2 hashCode: 109699471
s3 hashCode: 110001234
Runtime class of s1: Student

The output shows the results of overriding key methods from the Java Object class: s1: Student{id=101, name='Rahul'} is printed via the overridden toString(), giving a readable representation of the object’s fields; s1 equals s2: true and s1 equals s3: false come from the overridden equals(Object), which returns true for s1 and s2 because both have identical id and name, and false for s3 because its fields differ; the matching values s1 hashCode: 109699471 and s2 hashCode: 109699471 demonstrate the contract that equal objects produce the same hashCode(), while s3 hashCode: 110001234 differs due to different field values; finally, Runtime class of s1: Student (from getClass().getName()) confirms the object’s runtime type is Student, together proving the correct behavior of toString(), equals(), hashCode(), and getClass().

3. Conclusion

The Object class is the foundation of all Java classes. It provides essential methods that support core object-oriented principles such as abstraction, inheritance, polymorphism, and encapsulation.

By properly overriding methods like toString(), equals(), and hashCode(), developers can significantly improve debugging, object comparison, and collection performance. A solid understanding of the Object class is therefore critical for writing efficient, maintainable, and robust Java applications.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Java
👁 Photo of Yatin Batra
Yatin Batra
December 8th, 2025Last Updated: December 5th, 2025
0 233 3 minutes read

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz