- Object class is the super class of all the other classes in java.
- Every class is a subclass of the object class and hence they can use or override the methods present in the Object class.
- Since subclass objects can refer all the elements of parent class, all classes defined in java program can access methods and variables of Object class.
|Object ob = employee;
Object ob = getDetails(); //getDetails() may return object of any type
int names = new int(10);
Object obj = names; //includes arrays
Methods of Object class:
public final ClassgetClass()
Can be used to get the runtime class of the object. Also used to get the other information about the class Copies an array from the source array, beginning at the specified position, to the specified position of the destination array.
length – the number of array elements to be copied.
public int hashCode()
Returns a hash code value computed for the object.
public boolean equals(Object obj)
Returns true, if some object is equal to the current object. Otherwise, returns false.
This is most used when comparing two different objects. By default, two objects are considered equal when their hashcodes are same. Otherwise, programmer must override this equals method to clearly define on which basis the objects should be compared.
protected Object clone()
Creates and returns a copy of this object.
public String toString()
Returns the string representation of the object.
public final void notify()
Wakes up a thread, which is waiting on this objects’s monitor.
public final void notifyAll()
Wakes up all the threads, which are waiting on this objects’s monitor.
public final void wait(long timeout)
public final void wait(long timeout, int nanos)
public final void wait()
Causes the current thread to wait for the time if any specified, until another thread invokes the notify() method
protected void finalize()
Used by the garbage collector, when it learns that there are no more reference to the current object. Can be overridden by any subclass to perform the cleanup process.