Public interface cloneable
This class implements the cloneable interface to indicate that the object. Clone () method can legally copy such instances by field.
If you call the object clone method on an instance that does not implement the cloneable interface, a clonenotsupportedexception exception is thrown.
By convention, classes implementing this interface should use public methods to override object. Clone (which is protected ). See object. Clone () for details about rewriting this method.
Note that this interface does not contain the clone method. Therefore, it is impossible to clone an object by implementing this interface. Even if the clone method is called by reflection, it cannot be guaranteed that it will succeed.
The above is from Java Doc. Two conclusions can be drawn from this passage:
- You must implement the cloneable interface to be eligible to call the object. Clone () method.
- Implementing the cloneable interface is only one of the clone conditions. To be cloned, you must also rewrite the clone () method of the object because the object. the clone () method is protected and must be overwritten and changed to public.
Other considerations: If the cloned class references the objects of other classes, or even the object references other objects, you need to clone the entire object tree.