Object Class in Java and Its Methods - Quiz

Total: 6 questions

1. 

What is the Object class in Java?

The Object class (java.lang.Object) is the root of the Java class hierarchy. Every class implicitly extends Object, even without extends Object in its declaration, and Object itself is the only class with no superclass.

That is why the methods of the Object class (equals(), hashCode(), toString(), getClass(), clone(), wait(), notify(), and the rest) are available on every object, arrays included, and a variable of type Object can refer to any object. Primitives (int, double) are not objects and do not have these methods. Interfaces do not extend Object, but they implicitly declare its public methods.

2. 

What are the methods of the Object class in Java, and which of them cannot be overridden?

The Object class has 11 public and protected methods: equals(), hashCode(), toString(), getClass(), clone(), finalize(), notify(), notifyAll(), and three overloads of wait(): wait(), wait(long), wait(long, int).

Six methods are declared final and cannot be overridden: getClass(), notify(), notifyAll(), and all three wait() overloads. In practice you override equals(), hashCode() (always together with equals), and toString(); clone() rarely, and finalize() is deprecated and should not be overridden.

3. 

How do the default equals(), hashCode(), and toString() methods of the Object class work?

If a class does not override them, it uses the implementation inherited from Object:

  • equals() compares references, so it behaves exactly like ==: two different objects with equal fields are not equal;
  • hashCode() returns an identity hash code tied to the specific instance, so different objects almost always get different hash codes;
  • toString() returns getClass().getName() + "@" + Integer.toHexString(hashCode()), for example Point@1b6d3586.

To compare objects by their fields and get readable output, override these methods or declare a record (Java 16+), for which the compiler generates them automatically.

4. 

Why do you need to override hashCode() when you override equals() in Java?

The Object contract says that objects equal according to equals() must have the same hashCode(). The reverse is not required: unequal objects may share a hash code (a collision).

HashMap and HashSet first pick a bucket by hash code and only then compare elements with equals(). If you override only equals(), equal objects land in different buckets, and set.contains(new Point(1, 2)) returns false. The correct approach is to build both methods from the same fields:

@Override
public int hashCode() {
    return Objects.hash(x, y);
}
5. 

What are the clone() and finalize() methods of the Object class for, and why are they rarely used?

clone() (protected) creates a shallow copy of the object. The class must implement the Cloneable marker interface, or the Object implementation throws CloneNotSupportedException. A copy constructor or a static factory method is usually a cleaner choice.

finalize() may be called by the garbage collector before the object is reclaimed, but there is no guarantee when, or whether, it runs. It has been @Deprecated since Java 9 and deprecated for removal since Java 18 (JEP 421). To release resources, use try-with-resources with AutoCloseable, or java.lang.ref.Cleaner for rare edge cases.

6. 

Why are wait(), notify(), and notifyAll() declared in the Object class, and where can you call them?

In Java any object can act as a monitor (a lock object), and waiting and notifying are tied to the monitor, not to a thread. That is why these methods live in Object rather than in Thread. All of them are declared final.

You can call them only inside a synchronized block or method on the same object; otherwise an IllegalMonitorStateException is thrown. Call wait() inside a while loop that re-checks the condition, because of spurious wakeups:

synchronized (lock) {
    while (!ready) {
        lock.wait();
    }
}
Page 1 of 1