Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 (except Object) obey this convention, it will be the case that x.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.

...