Object Class In Java - Java Tutorial

Object Class In Java

BLUF: Mastering Object Class In Java is a key requirement for any Java developer. This lesson breaks down the object-oriented principles and syntax required to use this concept in real-world applications.
Write Once, Run Anywhere Tip: Object Class In Java

Java's versatility is unmatched. Learn how Object Class In Java fits into the Java ecosystem and improves your code structure in the tutorial below.

The Object class is inherently the superclass for all classes in Java, serving as the highest class in the Java hierarchy.

Utilizing the Object class can be advantageous when referencing an object of unknown type. It is important to understand that a reference variable of a parent class can point to an object of a child class, a concept known as upcasting.

Consider, for instance, a scenario where there exists a method named getObject which is designed to provide an object of varying types such as Employee or Student. In such cases, one can utilize a reference of the Object class to point to the specific object. As an illustration:

Example

Object obj=getObject();//we don't know what object will be returned from this method

The Object class offers various shared functionalities to all objects, including the ability to compare, clone, and notify objects.

Methods of Object class

The Object class provides many methods. They are as follows:
Method Description
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() throws CloneNotSupportedException 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 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)throws InterruptedException 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)throws InterruptedException 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()throws InterruptedException causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method).
protected void finalize()throws Throwable is invoked by the garbage collector before object is being garbage collected.

We will have the detailed learning of these methods in next chapters.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience