120 likes | 325 Views
Cloning & Cloneable Objects. What is Cloning?. Cloning is used to create a duplicate (a copy) of an existing object The clone is an exact copy (content-wise) of the original object Modifying the clone does not affect the original object. How?. First, review:
E N D
Cloning & Cloneable Objects CS-2851Dr. Mark L. Hornick
What is Cloning? Cloning is used to create a duplicate (a copy) of an existing object • The clone is an exact copy (content-wise) of the original object • Modifying the clone does not affect the original object CS-2851Dr. Mark L. Hornick
How? First, review: • Assume some object already exists A anObj = new A(); • anObj is a reference to the object • What does the following code do? A anotherObj = anObj; anObj.x = 1; anotherObj.x = 2; // what is value of anObj’s x-attribute now? A +x: int A() CS-2851Dr. Mark L. Hornick
Review:Multiple references A - x: int A() ctor • A anObj = new A();A anotherObj = anObj; • anotherObj is a 2nd reference to the same object referenced initially by anObj • anObj and anotherObj reference the same object • The same object can be modified via either reference: anObj.x = 3; // modify the object int n = anotherObj.x // “sees” x anOb j anotherOb j :A x CS-2851Dr. Mark L. Hornick
Java cloning • The Object class defines a clone() method • Which is inherited by every Java object derived from Object A anObj = new A(); A anotherObj = anObj.clone(); // works??? • Demo CS-2851Dr. Mark L. Hornick
The Cloneable interface • In order to be cloneable, an class must implement a clone() method • AND implement the Cloneable interface • Otherwise, the CloneNotSupportedException will be thrown if the clone() method is invoked Class A implements Cloneable {public Object clone() { A newObj = (A)super.clone(); // important! // other code goes here} }; CS-2851Dr. Mark L. Hornick
Object.clone() Clonable A - x: int +A() ctor # A clone() • The Object class’s clone() method makes an element-by-element (i.e. bitwise) copy of the object being cloned • If an element is a primitive or immutable object (int, String), this works OK • If the element is a reference to an object, only the reference is cloned – not the object • This is called a “shallow copy” anOb j anotherOb j :A Clone() :A x x CS-2851Dr. Mark L. Hornick
Sidebar:Objects & Inheritance B Class B {…}; Class A extends B {…}; anObj A = new A(); • Recall that the constructor of A is invoked automatically • Inside the A() constructor, we usually write super();// calls B() as the first statement • Why? - y: int B() ctor A - x: int A() ctor anOb j :A x y CS-2851Dr. Mark L. Hornick
Deep copy • If an object being cloned contains non-primitive or non-immutable elements • i.e. references to objects • The object must implement the “deep copy” within it’s own clone() method • Demo CS-2851Dr. Mark L. Hornick
JCF support for cloning Example: ArrayList • ArrayList implements Cloneable and clone() • The underlying structure is an array of elements • The clone() method makes a copy of the underlying array… • A cloned ArrayList will contain an independent copy of the underlying array, but… • If the array is an array of references to objects, both arrays will refer to the same set of objects (shallow copy!) • ArrayList clone() does NOT copy the objects • It does not assume that contained objects are cloneable CS-2851Dr. Mark L. Hornick