When a nonfinal class defines a clone()
method that fails to call super.clone()
, cloning a subclass of this class will produce an object of the wrong class.
The Java API [API 2011] for the clone()
method [API 2011] says:
By convention, the returned object should be obtained by calling
super.clone
. If a class and all of its superclasses (exceptObject
) obey this convention, it will be the case thatx.clone().getClass() == x.getClass()
.
...
Failing to call super.clone()
may result in cause a cloned object having to have the wrong type, with resulting unexpected or incorrect results when it is used.
...