Monday, January 12, 2009

Object Clonning

Object Cloning: Java object is an Reference Type. there is no way to create a copy of an existing object's attribute in to new object.
when you create object like

String S=new String("Hello Friends");
and S1=S; in this case S1 is a new Object not copy of an existing Object.

There is a Special method called Clone() in Object Class which allow to create a copy to an existing object. for this purpose you need to implement Cloneable interface which is markup interface(no Method).
Java allow two kinds to Clonning:
1: Shallow Clonning: Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the references are copied. Thus, if the object you are copying contains references to yet other objects, a shallow copy refers to the same subobjects.

class CoffeeCup implements Cloneable {
private int innerCoffee;
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
// This should never happen
throw new InternalError(e.toString());
}

}

public void add(int amount) {
innerCoffee += amount;
}

public int releaseOneSip(int sipSize) {
int sip = sipSize;
if (innerCoffee < sipSize) {
sip = innerCoffee;
}
innerCoffee -= sip;
return sip;
}

public int spillEntireContents() {
int all = innerCoffee;
innerCoffee = 0;
return all;
}
}

class Example1 {
public static void main(String[] args) {

CoffeeCup original = new CoffeeCup();
original.add(75); // Original now contains 75 ml of coffee
CoffeeCup copy = (CoffeeCup) original.clone();
copy.releaseOneSip(25); // Copy now contains 50 ml of coffee

// Figure 15-1 shows the heap at this point in the program

int origAmount = original.spillEntireContents();
int copyAmount = copy.spillEntireContents();
System.out.println("Original has " + origAmount + " ml of coffee.");
System.out.println("Copy has " + copyAmount + " ml of coffee.");
}

}
2: Deep Clonning: Deep copy is a complete duplicate copy of an object. If an object has references to other objects, complete new copies of those objects are also made. A deep copy generates a copy not only of the primitive values of the original object, but copies of all subobjects as well, all the way to the bottom. If you need a true, complete copy of the original object, then you will need to implement a full deep copy for the object.
class CoffeeCup implements Cloneable {

private Coffee innerCoffee = new Coffee(0);

public Object clone() {
CoffeeCup copyCup = null;
try {
copyCup = (CoffeeCup) super.clone();
}
catch (CloneNotSupportedException e) {
// this should never happen
throw new InternalError(e.toString());
}
copyCup.innerCoffee = (Coffee) innerCoffee.clone();
return copyCup;
}

public void add(int amount) {
innerCoffee.add(amount);
}

public int releaseOneSip(int sipSize) {
return innerCoffee.remove(sipSize);
}

public int spillEntireContents() {
return innerCoffee.removeAll();
}
class Example3 {
public static void main(String[] args) {

CoffeeCup original = new CoffeeCup();
original.add(75); // original now contains 75 ml of coffee
CoffeeCup copy = (CoffeeCup) original.clone();
copy.releaseOneSip(25);
// Copy now contains 50 ml of coffee.
// Original still has 75 ml of coffee.

// Figure 15-3 shows the heap at this point in the program

int origAmount = original.spillEntireContents();
int copyAmount = copy.spillEntireContents();
System.out.println("Original has " + origAmount
+ " ml of coffee.");
System.out.println("Copy has " + copyAmount
+ " ml of coffee.");
}
}

}

When you invoke Clone() method it Should Either return
  1. return an Object reference to a copy of the object upon which it is invoked, or
  2. throw CloneNotSupportedException
How to Disallow Clonning of Object: do not implement Cloneable Interface

Drawbacks of Clonning: Object Clonning allow to programmer steeling an object

No comments: