<<< clone method of the Product class | Index | Copy constructors >>> |
A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a bit-by-bit copy of instances of that class, known as member-wise shallow copy.
Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
By convention, classes that implement Cloneable interface should override Object.clone() (which is protected) with a public method.
When implementing your own clone(), the idea is to start with the object created by super.clone(), which is guaranteed to be of the correct class. Then populate any additional fields of the object to make a deep copy.
<<< clone method of the Product class | Index | Copy constructors >>> |