Last Updated: 28 March, 2023
In Java, the Object class is a predefined Java class that is by default a superclass of all the Java classes, so we do not need to import this class explicitly in any Java class. The Object class is available in the java.lang package and was introduced in JDK 1.0.
These methods can be overridden by subclasses to provide custom functionality.
Let's see the Object class constructor and its 11 methods in the below tables:
Constructor Name | Descriptions |
---|---|
public Object() |
The Object class has only one default constructor. |
Method Name | Descriptions |
---|---|
public final Class getClass() |
returns the Class class object of this object. The Class class can further be used to get the metadata of this class. |
public int hashCode() |
returns the hashcode number for this object. |
public boolean equals(Object obj) |
compares the given object to this object. |
protected Object clone() |
creates and returns the exact copy (clone) of this object. |
public String toString() |
returns the string representation of this object. |
public final void notify() |
wakes up a single thread, waiting on this object's monitor. |
public final void notifyAll() |
wakes up all the threads, waiting on this object's monitor. |
public final void wait(long timeout) |
causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method) |
public final void wait(long timeout, int nanos) |
causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method). |
public final void wait() |
causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method). |
protected void finalize() |
is invoked by the garbage collector before object is being garbage collected. |
When defining a new class in Java, it is often a good idea to override some of the methods in the Object class to provide custom implementations that are more appropriate for the new class. For example, if a class represents a person, it might be appropriate to override the toString() method to return the person's name instead of the default string representation provided by the Object class.
That's all guys, hope this Java article is helpful for you.
Happy Learning.
feedback@javabytechie.com
Why is the Object class the default superclass of all the Java classes?
An Object class is the default superclass of all the Java classes because the Object class contains the basic functionality methods such as hashCode(), clone(), toString(), wait(), notify(), etc. that are common to every Java class and are implicitly available in all the Java classes. Users can override these methods if required; otherwise, they can use the default implementation.