Difference between revisions of "Cloning"

From Suhrid.net Wiki
Jump to navigationJump to search
(Created page with "* 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 ...")
 
Line 3: Line 3:
 
* 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 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.
 
* 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.

Revision as of 16:14, 22 June 2012

  • 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.