1 / 10

Cloning & Cloneable Objects

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:

haven
Download Presentation

Cloning & Cloneable Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cloning & Cloneable Objects CS-2851Dr. Mark L. Hornick

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

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

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

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

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

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

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

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

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

More Related