Rajinder Menu

How to write a Copy Constructor in java using Deep copy.


This example shows how to create a copy of an object using copy constructor in java. Copy constructor in java is a constructor which accepts only one argument and this argument is of the same type to which this constructor belongs to.

Following example has a Car class and it contains an object of Wheel class as one of its attributes. In the copy constructor of Car class we are copying the attributes of  argument Car object to the new Car object. Note how we are copying  the wheel object in the copy constructor of car. Here we are using deep copy to copy the wheel object instead of just assigning the references as we did in Simple constructor of Car class as given below.

public class Car {
    private int year;
    private String make;
    private Wheel wheel;

    /*Simple Constructor*/
    Car(int year, String make,Wheel wheel){
     this.year = year;
     this.make = make;
     this.wheel = wheel;
 
    }
 
    /*Copy Constructor.Note argument is also of type Car.*/
    Car(Car otherCar){
        this.year=otherCar.getYear();
        this.make=otherCar.getMake();
 
      /*Copying wheel object using Deep Copy*/
       Wheel wheel = new Wheel(otherCar.getWheel().getCompany());
       this.wheel=wheel;
    }
 
    public void setYear(int year){
        this.year =  year;
    }

    public int getYear(){
        return year;
    }
    public void setMake(String make){
        this.make=make;
    }

    public String getMake(){
        return make;
    }

   public void setWheel(Wheel wheel){
        this.wheel=wheel;
    }

    public Wheel getWheel(){
        return wheel;
    }
 
public static void main(String args[]){

   Wheel wheel = new Wheel("Ceat");
   Car car1 = new Car(2011,"Toyota",wheel);

   System.out.println("Car1 description:");
   System.out.println("    Year:"+car1.getYear());
   System.out.println("    Make:"+car1.getMake());
   System.out.println("    Wheel Description:");
   System.out.println("        Company:"+car1.getWheel().getCompany());

   /*Invoking copy constructor.Creating copy of car1.*/
    Car car2 = new Car(car1);

   System.out.println("Car2 description(Copy of car1):");
   System.out.println("    Year:"+car2.getYear());
   System.out.println("    Make:"+car2.getMake());
   System.out.println("    Wheel Description:");
   System.out.println("        Company:"+car1.getWheel().getCompany());
  }
}

class Wheel{

private String company;

Wheel(String company){
this.company = company;
}
public void setCompany(String company){
        this.company=company;
    }

    public String getCompany(){
        return company;
    }

}
Output:

Car1 description:
    Year:2011
    Make:Toyota
    Wheel Description:
        Company:Ceat

Car2 description(Copy of car1):
    Year:2011
    Make:Toyota
    Wheel Description:
        Company:Ceat



I would like to know your comments and if you liked the article then please share it on social networking buttons.




You may want to read:
How to write a Copy Constructor using Copy Constructor.
Copying Object by Copy Constructors in Java.
Copying Object by deep copy in Java.
Copying Object by shallow Copy in Java.

1 comment: