In shallow copy you read that while making copy of objects in java you only copy reference variables instead of copying all attributes of component objects. As a result of this both original and copy are not independent and share the reference to component objects.
But in deep copy you do not copy the references of component objects, instead you copy their attributes along with the attributes of container object.
Due to this both original and copy are independent and do not share the reference to component objects.
Lets see how deep copy is implemented. This example is almost same as you have seen in article for shallow copy except that createShallowCopy() method has been changed to createDeepCopy() method.
Example:
Engine .java
Note that here in createDeepCopy() method you are not copying references of engine object as you did in shallow copy like:
car.engine=anotherCar.engine; [in shallow copy]
But you are creating a new Engine object and copying the attributes of engine of anotherCar to this newly created object and then assigning it as car engine.
Due to this now car and anotherCar has separate engines.
That's why when you changed the location of engine of car2 in main(), it did not effect the engine location of car1 as seen in the output below:
Output:
Car 1 Details:
Make=Maruti
****Engine Details****
Engine Location=Front
Car 2 Details:
Make=Maruti
****Engine Details****
Engine Location=Front
*******************After Change*******************************
Car 1 Details:
Make=Maruti
****Engine Details****
Engine Location=Front (remain Unchanged)
Car 2 Details:
Make=Maruti
****Engine Details****
Engine Location=Back
But in deep copy you do not copy the references of component objects, instead you copy their attributes along with the attributes of container object.
Due to this both original and copy are independent and do not share the reference to component objects.
Lets see how deep copy is implemented. This example is almost same as you have seen in article for shallow copy except that createShallowCopy() method has been changed to createDeepCopy() method.
Example:
Engine .java
class Engine {
private String engineLocation;
Engine(){
}
Engine(String engineLocation){
this.engineLocation=engineLocation;
}
public void setEngineLocation(String engineLocation){
this.engineLocation=engineLocation;
}
public String getEngineLocation(){
return engineLocation;
}
}
Car.javaprivate String engineLocation;
Engine(){
}
Engine(String engineLocation){
this.engineLocation=engineLocation;
}
public void setEngineLocation(String engineLocation){
this.engineLocation=engineLocation;
}
public String getEngineLocation(){
return engineLocation;
}
}
public class Car {
private String make;
private Engine engine;
Car(){
}
Car(String make,Engine engine){
this.make=make;
this.engine=engine;
}
/*Creating copy of Car object*/
public static Car createDeepCopy(Car anotherCar){
Car car =new Car();
car.make=anotherCar.make;
/*Making deep copy of Engine object*/
Engine engTemp = new Engine();
engTemp.setEngineLocation(anotherCar.getEngine().getEngineLocation());
car.engine = engTemp;
return car;
}
public void setMake(String make){
this.make=make;
}
public String getMake(){
return make;
}
public void setEngine(Engine engine){
this.engine=engine;
}
public Engine getEngine(){
return engine;
}
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car1 = new Car("Maruti",engine);
/*Creating a copy of Car object*/
Car car2= Car.createDeepCopy(car1);
System.out.println("Car 1 Details:");
System.out.println("Make="+car1.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car1.getEngine().getEngineLocation());
System.out.println("Car 2 Details:");
System.out.println("Make="+car2.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car2.getEngine().getEngineLocation());
/*Changing engine of car2*/
car2.getEngine().setEngineLocation("Back");
System.out.println("*******************After Change*******************************");
System.out.println("Car 1 Details:");
System.out.println("Make="+car1.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car1.getEngine().getEngineLocation());
System.out.println("Car 2 Details:");
System.out.println("Make="+car2.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car2.getEngine().getEngineLocation());
}
}
private String make;
private Engine engine;
Car(){
}
Car(String make,Engine engine){
this.make=make;
this.engine=engine;
}
/*Creating copy of Car object*/
public static Car createDeepCopy(Car anotherCar){
Car car =new Car();
car.make=anotherCar.make;
/*Making deep copy of Engine object*/
Engine engTemp = new Engine();
engTemp.setEngineLocation(anotherCar.getEngine().getEngineLocation());
car.engine = engTemp;
return car;
}
public void setMake(String make){
this.make=make;
}
public String getMake(){
return make;
}
public void setEngine(Engine engine){
this.engine=engine;
}
public Engine getEngine(){
return engine;
}
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car1 = new Car("Maruti",engine);
/*Creating a copy of Car object*/
Car car2= Car.createDeepCopy(car1);
System.out.println("Car 1 Details:");
System.out.println("Make="+car1.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car1.getEngine().getEngineLocation());
System.out.println("Car 2 Details:");
System.out.println("Make="+car2.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car2.getEngine().getEngineLocation());
/*Changing engine of car2*/
car2.getEngine().setEngineLocation("Back");
System.out.println("*******************After Change*******************************");
System.out.println("Car 1 Details:");
System.out.println("Make="+car1.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car1.getEngine().getEngineLocation());
System.out.println("Car 2 Details:");
System.out.println("Make="+car2.getMake());
System.out.println("****Engine Details****");
System.out.println(" Engine Location="+car2.getEngine().getEngineLocation());
}
}
Note that here in createDeepCopy() method you are not copying references of engine object as you did in shallow copy like:
car.engine=anotherCar.engine; [in shallow copy]
But you are creating a new Engine object and copying the attributes of engine of anotherCar to this newly created object and then assigning it as car engine.
Due to this now car and anotherCar has separate engines.
That's why when you changed the location of engine of car2 in main(), it did not effect the engine location of car1 as seen in the output below:
Output:
Car 1 Details:
Make=Maruti
****Engine Details****
Engine Location=Front
Car 2 Details:
Make=Maruti
****Engine Details****
Engine Location=Front
*******************After Change*******************************
Car 1 Details:
Make=Maruti
****Engine Details****
Engine Location=Front (remain Unchanged)
Car 2 Details:
Make=Maruti
****Engine Details****
Engine Location=Back
I would like to know your comments and if you liked the article then please share it on social networking buttons.
You may like to read:
How to write a Copy Constructor using Deep copy.
How to write a Copy Constructor using Copy Constructor.
Copying Object by shallow Copy.
Copying Object by Copy Constructors.
No comments:
Post a Comment