| SE450: Clone: clone() [6/32] | ![]() ![]() ![]() |
The intent is that
x.clone() != x x.clone().getClass() == x.getClass() x.clone().equals(x)
The last intent is not a requirement, but is generally true.
By convention, you obtain the returned object by calling
super.clone(). If all superclasses do this,
then x.clone().getClass() == x.getClass() will
be true.
Object doesn't implement Cloneable
itself. (Object.clone is protected.)
The Cloneable interface is meant to be a
mixin interface, however it doesn't do a very good
job of this.
Parts of Java that support cloning:
Cloneable interface
(note the misspelling)
Object.clone method (or your superclass's
clone method)
CloneNotSupportedException, to signal the
clone method shouldn't have been called.
You can:
Cloneable and declare
clone() to throw no exceptions).
Clonable, but may throw
CloneNotSupportedException). This is the
case for a container class whose contents may or may not
be clonable.
Clonable; do nothing, or override
clone to do the right thing for your class)
Clonable;
override clone to always throw
CloneNotSupportedException)