Methods in Object class

As known java.lang.Object is base class, of all classes in Java.

Below are the methods of Object class, that means each and every object can invoke below methods.

Method Declaration Method Details
1 protected Object clone() creates a copy of current object, and returns
2 boolean equals(Object obj); This method returns whether the object(sent as parameter) is equal to invoking object.
3 protected void finalize(); When there are no more references to the object, garbage collector, may invoke this method.(This method acts just like destructor of the c plus plus class)
4 Class getClass(); returns runtime class of the invoking.
5 int hashCode(); returns unique hash code value of invoking object. This is used to uniquely identify each object.
6 final void notify(); wakes up single thread that is waiting on this object’s monitor.
7 final void notifyAll(); wakes up all threads that are waiting on this object’s monitor.
8 String toString(); returns string representation of invoking object.
9 void wait(); causes current thread to wait until another thread invokes notify() or notifyAll() method for this object.
10 void wait(long timeout_millis); causes current thread to wait until either another thread invokes the notify() or notifyAll() method for this object, or specified amount of time(sent as parameter) has been elapsed.
11 void wait(long timeout_millis, int timeout_nanos); causes current thread to wait until another thread invokes notify() or notifyAll() method for theobject, or some other thread interrupts the current thread, or a certain amount of real time(sent as parameters) has been elapsed.

final methods in Object class

Below are final methods in Object class
getClass(), wait(), wait(long), wait(long,int), notify(), notifyAll() methods are final, and cannot be overridden.

Below non final methods can be overridden.
clone(), equals(), hashCode(), toString() methods are non final, and can be overridden by any class

Below is an example showing that java.lang.Object is Base class

class Test{


public class ObjectSuperClass {
	public static void main(String args[])
	Test obj = new Test();
	Class c = obj.getClass();
	Class sc = c.getSuperclass();

	System.out.println("Super class of Test is: "+sc.getName());

Super class of Test is: java.lang.Object

Also note that instance of java.lang.Object class can be created.

No class can exist in Java, which is not Derived class of Object class. All Java classes, by default inherit from java.lang.Object class.

An object of java.lang.Object class, can refer to any object in Java.
Since Object is base class of all classes, Conversion of any object to String, checking for equality of objects, etc… is being supported by each and every object in Java

You may also like to read:
Can we have static method in abstract class
How many types of inheritance in Java
How to implement Adapter pattern using inheritance

One thought on “Methods in Object class”

Leave a Reply