Cloneable
is the marker Interface
that indicates that clone()
may be called on an object. Overriding
clone()
without implementing Cloneable
can be helpful if you want to control how subclasses clone themselves, but otherwise,
it’s probably a mistake.
The usual convention for Object.clone()
according to Oracle’s Javadoc is:
-
x.clone() != x
-
x.clone().getClass() == x.getClass()
-
x.clone().equals(x)
Obtaining the object that will be returned by calling super.clone()
helps to satisfy those invariants:
-
super.clone()
returns a new object instance
-
super.clone()
returns an object of the same type as the one clone()
was called on
-
Object.clone()
performs a shallow copy of the object’s state.