Object Class

In Java, everything is an object, but did you know that there is one class at the top of all Java classes? That class is the Object class. Whether you’re creating your own classes or using built-in Java libraries, you’re always working with objects that eventually extend from this class.

The Object class is the root class of the Java class hierarchy. Every class in Java implicitly inherits from the Object class, even if you don’t explicitly declare it. This means that all classes in Java are subclasses of Object, whether they’re part of the Java standard library or your own custom classes. Here’s a simple breakdown:

  • All classes inherit from Object.
  • It provides a set of methods that every Java class inherits, even if you don’t define them in your class.

Methods of the Object Class

Method Syntax Description Example
toString() public String toString() Returns a string representation of the object. Person p = new Person(“Alice”);
System.out.println(p.toString()); // Output: Person{name=’Alice’}
equals(Object obj) public boolean equals(Object obj) Compares the current object with another object for equality. Person p1 = new Person(“Alice”);
Person p2 = new Person(“Alice”);
System.out.println(p1.equals(p2)); // Output: true
hashCode() public int hashCode() Returns a hash code value for the object. Person p = new Person(“Alice”);
System.out.println(p.hashCode());
getClass() public final Class<?> getClass() Returns the runtime class of the object. Person p = new Person(“Alice”);
System.out.println(p.getClass().getName()); // Output: Person
clone() protected Object clone() throws CloneNotSupportedException Creates and returns a copy of the current object. Person p1 = new Person(“Alice”);
Person p2 = (Person) p1.clone();
System.out.println(p2.name); // Output: Alice
finalize() protected void finalize() throws Throwable Called by the garbage collector when the object is no longer reachable. Person p = new Person(“Alice”);
p.finalize();
getName() public String getName() Returns the name of the class of the object. Person p = new Person(“Alice”);
System.out.println(p.getClass().getName()); // Output: Person
notify() public final void notify() Wakes up a single thread that is waiting on this object’s monitor. Object obj = new Object();
obj.notify();
notifyAll() public final void notifyAll() Wakes up all threads that are waiting on this object’s monitor. Object obj = new Object();
obj.notifyAll();
wait() public final void wait() throws InterruptedException Causes the current thread to wait until it is notified or interrupted. Object obj = new Object();
obj.wait();
wait(long timeout) public final void wait(long timeout) throws InterruptedException Causes the current thread to wait for the specified time in milliseconds. Object obj = new Object();
obj.wait(1000);
wait(long timeout, int nanos) public final void wait(long timeout, int nanos) throws InterruptedException Causes the current thread to wait for the specified time in milliseconds plus nanoseconds. Object obj = new Object();
obj.wait(1000, 500);