An object class is the superclass of all the classes in Java. It is the topmost class from which every other class in Java is derived whether directly or indirectly. The class that does not extend any other class is directly derived from the object class and the class that extends other class is indirectly derived from the object class.
It can be used to refer any object whose type is not known. Every class in Java has the methods of the object class. An object class is the root of the inheritance hierarchy in Java.
Methods of the Object class
The methods provided by an object class are:
- Public final class getClass()
This method returns the class object of this object. It is used to get metadata of this class and the actual runtime class of the object. It cannot be overridden.
- Public int hashCode()
This method returns the hashCode number of the object and the distinct integers for distinct objects. It is native and uses an algorithm to convert the internal address of an object to an integer.
- Public Boolean equals (Object obj)
This method makes a comparison between this object and the given object.
- Protected Object clone()
This method is used to make and return the clone of this object.
- Public String toString()
This method converts an object to String and also provides String representation of an object. It returns a string consisting of the name of the class of which the object in an instance.
- Public final void notify()
This method wakes up a single thread but does not give up a lock on a resource.
- Public final void notifyAll()
This method wakes up all the threads on the same object.
- Public final void wait()
This method causes the current thread to go to sleep and wait until some other thread calls notify ().
- Protected void finalize()
This method is called by the garbage collector when it decides that there are no more references to the object. It is called only for a single time before an object is being garbage collected.
Object Cloning in Java
Object cloning is the process of creating an exact copy of an object. An object is cloned in Java by using the clone() method. It is an object class method. The programmer must first implement the java.lang.Cloneable interface to the class whose object is going to be cloned.
If not done so, the clone() method will throw a CloneNotSupportedException. By using the clone() method, a new instance of the class of the current object is created. The method also initializes all the fields of the object with the exact contents of the corresponding fields of the object.
Why use clone() method?
The clone() method copies the exact values of one object to another. If we don’t use the clone() method, to create an object as same as another object we must first use a new keyword and then assign the values of one object to another.
This process takes up a lot of processing time. So to save the extra processing time, the clone() method is used as there is no need to write an explicit code to copy the value of one object to another using the method.
Advantages of object cloning
The clone() method is one of the most used and an easy way to copy objects. The various advantages of using the method are:
- The clone() method removes the need for writing lengthy and repetitive codes to create an object with the same variables of an already created object.
- The clone() method is an easy way to copy objects in the most efficient way if we are using it to a project that has already been created.
- Along with copying an object, it is also the fastest method to clone an array.
- The clone() method saves the extra processing time required if we copy objects using the copy constructor.
Disadvantages of object cloning
- A lot of the syntaxes of the code have to be changed in order to use the clone() method. The programmer has to implement a Cloneable interface, define the clone() method, and handle the CloneNotSupportedException error, and call the Object.clone().
- The programmer must tell the JVM that the clone() method can be performed on the object by implementing the Cloneable interface even if it does not have any methods in it.
- The Object.clone () is protected, that means it is not visible and easily accessible. So the programmer must provide their own clone() method and call Object.clone() indirectly from the method.
- The programmer does not have any control over the object construction as the Object.clone() does not invoke a constructor.
- The super.clone() chain will fail if there is no clone() method defined in the parent class of the child class in which the programmer wants to write a clone method.
- Shallow copying is only supported by the Object.clone() in Java. The programmer must first perform method overriding if a deep cloning is needed to be done.