I would like to know the following:
Cloneable
means we can have a clone or a copy of objects, by implementing the Cloneable
interface. What are the advantages and disadvantages of doing that?Cloneable is an interface that is used to create the exact copy of an object. It exists in java. lang package. A class must implement the Cloneable interface if we want to create the clone of the class object.
Cloneable interface is implemented by a class to make Object. clone() method valid thereby making field-for-field copy. This interface allows the implementing class to have its objects to be cloned instead of using a new operator.
clone() method acts like a copy constructor. It creates and returns a copy of the object. Since the Object class has the clone method (protected) you cannot use it in all your classes. The class which you want to be cloned should implement clone method and overwrite it.
clone() is a method in the Java programming language for object duplication. In Java, objects are manipulated through reference variables, and there is no operator for copying an object—the assignment operator duplicates the reference, not the object. The clone() method provides this missing functionality.
The first thing you should know about Cloneable
is - don't use it.
It is very hard to implement cloning with Cloneable
right, and the effort is not worth it.
Instead of that use some other options, like apache-commons SerializationUtils
(deep-clone) or BeanUtils
(shallow-clone), or simply use a copy-constructor.
See here for the views of Josh Bloch about cloning with Cloneable
, which explains the many drawbacks of the approach. (Joshua Bloch was a Sun employee, and led the development of numerous Java features.)
Cloneable itself is unfortunately just a marker-interface, that is: it does not define the clone() method.
What is does, is change the behavior of the protected Object.clone() method, which will throw a CloneNotSupportedException for classes that do not implement Cloneable, and perform a member-wise shallow copy for classes that do.
Even if this is the behavior you are looking for, you'll still need to implement your own clone() method in order to make it public.
When implementing your own clone(), the idea is to start with the object create by super.clone(), which is guaranteed to be of the correct class, and then do any additional population of fields in case a shallow copy is not what you want. Calling a constructor from clone() would be problematic as this would break inheritance in case a subclass wants to add its own additional cloneable logic; if it were to call super.clone() it would get an object of the wrong class in this case.
This approach bypasses any logic that may be defined in your constructors though, which could potentially be problematic.
Another problem is that any subclasses that forget to override clone() will automatically inherit the default shallow copy, which is likely not what you want in case of mutable state (which will now be shared between the source and the copy).
Most developers don't use Cloneable for these reasons, and simply implement a copy constructor instead.
For more information and potential pitfalls of Cloneable, I highly recommend the book Effective Java by Joshua Bloch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With