up previous next contents
Next: Extending classes: how and Up: Extending Classes Previous: Abstract Classes and methods   Contents

Subsections

Cloning objects

  • A clone method returns a new object whose initial state is a copy of the current state of the object on which clone was invoked.
  • Subsequent changes to the clone will not affect the state of the original.
  • Object.clone()

Three major considerations in writing a clone method

  • Empty Cloneable interface.
  • Object.clone() method.
  • CloneNotSupportedException.

Four different attitudes a class can have towards clone

  • Support clone.
  • Conditionally support clone.
  • Allow subclasses to support clone but don't publicly support it.
  • Forbid clone.


up previous next contents
Next: Extending classes: how and Up: Extending Classes Previous: Abstract Classes and methods   Contents