How do I design a class so that it supports cloning? -Define a class that implements Clonable interface.
- Override the clone() method of Object class. Some classes my need deep cloning and some shallow cloning.
- Shallow cloning is most appropriate in the situation where a data element directly represents a value. Deep cloning builds a new reference and a new object to refer to a new object.
- The Cloneable should be implemented.
- clone( ) should be overridden in which super.clone( ) is called.
- The exceptions should be caught in super.clone() so that the overridden clone( )doesn’t throw any exceptions.
|