VOOZH about

URL: https://www.geeksforgeeks.org/java/object-class-in-java/

⇱ Object Class in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Object Class in Java

Last Updated : 7 Apr, 2026

Object class (in Java.lang) is the root of the Java class hierarchy. Every class in Java either directly or indirectly extends Object. It provides essential methods like toString(), equals(), hashCode(), clone() and several others that support object comparison, hashing, debugging, cloning and synchronization.

  • Acts as the root of all Java classes
  • Defines essential methods shared by all objects
  • Supports thread communication (wait(), notify(), notifyAll())

Example: Using toString() and 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:

👁 Object Class Methods in Java

1. toString() Method

toString() provides a String representation of an object and is used to convert an object to a String.


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. 


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

equals()method compares the given object with the current object. It is recommended to override this method to define custom equality conditions. 


Output
true

Explanation: equals() compares objects based on content rather than reference. Must be overridden when custom comparison logic is needed.

4. getClass() method

getClass() method returns the class object of "this" object and is used to get the actual runtime class of the object.


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.


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.


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()

These methods are related to thread Communication in Java. They are used to make threads wait or notify others in concurrent programming.


Output
The Hitchhiker's Guide to the Galaxy by Douglas Adams (1979)
The Hitchhiker's Guide to the Galaxy by Douglas Adams (1979)
b1 equals b2: true
b1 hash code: 1840214527
b2 hash code: 1840214527

Explanation: The above example demonstrates the use of toString(), equals(), hashCode() and clone() methods in the Book class.

Comment
Article Tags:
Article Tags: