Cloning

From Suhrid.net Wiki
Revision as of 16:14, 22 June 2012 by Suhridk (talk | contribs)
Jump to navigationJump to search
  • There is no syntactical way to copy an object in Java. The assignment operator just's duplicates the reference.
  • Cloning and the clone() method provides a way to perform such copying.
  • The object class has a clone() method which is protected. So it has to be overriden in the implementing class in order to use it.
  • The default implementation can call super.clone() - but this means the class has to implement the Cloneable marker interface.
  • Object's clone() performs what is called a "shallow" copy - typically one level of copying.
  • Deep copying means copying containing referenced objects as well. Of course we can keep going deeper, so "shallow" and "deep" are subjective terms and it is upto developer to implement.