Object class

Object class is present in the java.lang package. It is the super class for all the classes directly or indirectly. If a class is not extended any class then for the class Object class is super class. If a class extends an other class then for this class Object class is indirect super class.

Object class has 12 methods which given below.

  1. private static native void registerNatives(): It is a private method so we no need to consider it. It is implemented in the other language(C/C++).
  2. public final native Class<?> getClass(): It is a native method and implementation in other language(C/C++). Which is used for the getting the class level information. It also provides the meta data of a class. It is a final class and it can’t be overridden.Example:
  3. public native int hashCode(): It is a native method. For every object JVM generates a unique number which is different for different objects. This distinct number is generated by the hashcode. hashCode method returns the unsigned hex decimal integer. This number is formed by hashing the internal address of the object by using an algorithm. The main advantage of hashing is searching becomes easy. This method can be overridden, when it overridden we have to make sure that each object will have different hashcode.
  4. public boolean equals(Object obj): It is used for compare a object with ‘this’ object. By default implementation in Object class of this method is compare the given object reference with ‘this’ object reference (Not contents of the object).  It is recommended that whenever this method is overridden then override hashcode method for make sure same content objects has same hash code.
  5. protected native Object clone(): It is used for the make a copy of the object. If we want to make a copy of an object with using this method then class must implemented with Cloneable interface(Marker Interface) otherwise will get a CloneNotSupportedException.  By using clone method we can do 2 types of object copies. 1. Shallow cloning ( When we use it, it can copy only instance variables but not reference variables. i.e if we try to modify using this reference variables from copied object then actual object of that reference will be modified). 2. Deep cloning (When we use it, it can copy instance variables and also reference variable. i.e if we modify the data with reference variable from the copied object then it won’t modify the actual object.)
  6. public String toString(): It is used for converting the object to String. By default implementation of this method in Object class is to convert the object reference to String. i.e class name + @+ hex integer. Not actual value of object. It is recommended to override to get the content of object to convert String.
  7. public final void wait(): It is used in the multi-threaded programming. It keeps the current thread in sleep and release the lock until another thread enters the same monitor and calls notify.
  8. public final void wait(long timeout): It keeps the current thread in sleep for specified time and release the lock on it. Here time is milliseconds.
  9. public final void wait(long timout, int nanos): It keeps the current thread in sleep for specified milliseconds and nanoseconds and releases the lock on it.
  10. public final native void notify(): It wakes up(notify) one thread which is called wait on same object. notify method just wakes up the thread but not release the lock until its execution finished.
  11. public final native void notifyAll():It wakes up all threads which are called wait on same object. It also doesn’t release the lock until execution is finished.

Note: Above 5 methods (from 7 to 11) used in synchronized block only.

12.protected void finalize(): This method is called just before an object is garbage collected. It is called by the Garbage Collector on an object when garbage collector determines that there are no more references to the object. This method we need to override whenever we need to clean up the resources used in the object and also to avoid the memory leaks.