I would like to know the following:
Cloneablemeans we can have a clone or a copy of objects, by
implementing theCloneableinterface. What are the advantages and
disadvantages of doing that?- How does the recursive cloning happen if the object is a
composite object?
The first thing you should know about
Cloneableis – don’t use it.It is very hard to implement cloning with
Cloneableright, and the effort is not worth it.Instead of that use some other options, like apache-commons
SerializationUtils(deep-clone) orBeanUtils(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.)