How to clone an object?
To enable our object to be cloned we need to override
Object class clone method. We can also add a
java.lang.Cloneable interface to our class, this interface is an empty interface. When we call the
clone() method we need the add a try-catch block to catch the
CloneNotSupportedException. This exception will be thrown if we tried to clone an object that doesn't suppose to be cloned.
clone() method does a stateful, shallow copy down inside the Java Virtual Machine (JVM). It creates a new object and copies all the fields from the old object into the newly created object.