Definition of Object Class in Java
Object class is the root class in every inheritance tree that can exist since JDK 1.0. Every class in Java inherits the properties and methods of Object class either direct or indirectly that is, for instance, in case a class C1 extends other class C2, then since Object class is a parent of all class, C1 indirectly inherits Object class otherwise by default every class being created in Java extends Object class by default. Thus every data type object in Java is a type of Object class objects; thus, an object of Object class can hold any type of data inside it.
How does Object Class work in Java?
Object class in java.lang package is the top in the class hierarchy tree; thus, every class is like direct or indirect descendant to this class. This implies that all classes inherit the instance methods from the Object class and can be called using their own objects. But to use the inherited methods, a class needs to override them inside them.
Also, the Object class act as a container class for all the data types; thus, in case the data type of one object is unknown, it can be easily referred to using object of Object class since parent class object can be easily used to refer to the child class objects, known as upcasting.
Methods of Object Class in Java
Below is the list of instance methods available in the Object class:
1. toString()
Syntax:
public String toString()
This function is used to extract the String representation of an object. This String representation can be easily modified according to the need For e.g., in case there is a class Office that needs that its head of that branch must be displayed with location whenever one calls this function. Thus representation depends entirely on the object and useful while debugging.
2. hashCode()
Syntax:
public int hashCode()
This function returns the hexadecimal representation of the object’s memory address, which is helpful in the case of equating 2 objects as 2 objects are said to equal only if their hashcode matched, as per implementation present in the Object class. But if one overrides the equals() method of Object class, super implementation becomes inactive for that class’s objects.
3. equals(Object obj)
Syntax:
public boolean equals(Object obj)
This method is used to override the default implementation of the equals method to compare 2 objects using their hashcode and returns true or false accordingly. In case you want to implement your own logic to compare 2 objects of your class, you must override the equals method.
4. getClass()
Syntax:
public final Class getClass()
This method returns the reference of a Class object that helps to retrieve various information about the current class. Below are examples of instance methods of Class
- getSimpleName(): Returns the name of the class.
- getSuperClass(): Returns the reference of the superclass of the specified class.
- getInterfaces(): Returns the array of references of Class type for all the interfaces that have been implemented in a specified class.
- isAnnotation(): Returns True is the specified class is an annotation otherwise false.
- getFields(): Returns the list of fields of the specified class.
- getMethods(): Returns the list of instance methods of the class.
5. finalize()
Syntax:
protected void finalize() throws Throwable
This method is called whenever JVM depicts that there is no more reference exit to that object so that garbage collection can be performed. There is nothing defined in finalize function in the Object class and returns nothing. Thus the subclasses can implement it to perform some action but must not rely on when it will be invoked as the thread that invokes it does not hold a user-visible authentication lock.
Any exception occurs while finalize() halts its execution; thus, it must be handled with precautions.
6. clone()
Syntax:
protected Object clone() throws CloneNotSupportedException
This method of Object class is meant to be overridden in a subclass in case Cloneable Interface is implemented in it, and this method is used to clone, i.e. creating a copy of that object with values in member variables, using aobj.clone() syntax. In case one class does not implement the Cloneable interface, CloneNotSupportedException is thrown.
By default, this method checks if the Cloneable interface has been implemented in this case, but in case you want to override this method according to your logic, you can easily do this using the following syntax:-
protected Object clone() throws CloneNotSupportedException
or
public Object clone() throws CloneNotSupportedException
Example:
package Try;
public class Desk implements Cloneable{
private int id;
private String Mid;
Desk(int id,String mid){
this.id=id;
this.Mid = mid;
}
@Override
public String toString()
{
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
@Override
public int hashCode()
{
return id;
}
public boolean equals(Desk dd){
return this.id == dd.id;
}
@Override
protected void finalize()
{
System.out.println("Lets call Fialize");
}
@Override
protected Desk clone() {
return this;
}
public static void main(String[] args){
Desk d1=new Desk(123,"344");
Desk d2=new Desk(234,"344");
System.out.println("toString Representation " + d1.toString());
System.out.println("HashCode for this object "+d1.hashCode());
System.out.println("Comparing 2 objects using equals method "+ d1.equals(d2));
Desk d3=d2.clone(); // cloning d2 object
System.out.println("Comparing clone object with original "+d2.equals(d3));
d1=d2=d3=null;
System.gc();
}
}
Output:
Below three methods of Object class are used in case you need to implement multithreading behavior and need synchronization between different threads.
7. wait()
This method is used to put the current thread in the waiting state until any other thread notifies. The time needs to be specified in the function in milliseconds for which you need to resume the thread execution. There are three definitions for this function as given below:
- public final void wait()
- public final void wait(long timeout)
- public final void wait(long timeout, int nanos)
Note: InterruptedException is thrown by this method.
8. notify()
Syntax:
public final void notify()
9. notifyAll()
This method is used to wake up all the threads waiting in the waiting queue.
Syntax:
public final void notifyAll()
Conclusion
Object class is topmost in the class hierarchy of every class in Java; thus, every class inherits its instance methods and can use them after overriding them according to the scenarios. Also, its object can be used as a reference variable for every class object using the upcasting concept.
Recommended Articles
This is a guide to Object Class in Java. Here we discuss the definition and how object class works in java, along with methods and examples. you may also have a look at the following articles to learn more –
- Non Access Modifiers in Java
- Anonymous Class in Java
- Nested if Statements in Java
- Dynamic Array in Java
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses