How to write a Copy Constructor in Java using Copy Constructor.


 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 instead of using simple constructor of Wheel class as we did in this example, we are using copy constructor for Wheel class.

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*/
    Car(Car otherCar){
        this.year=otherCar.getYear();
        this.make=otherCar.getMake();
   
        /*Using  Copy Constructor of Wheel*/
        Wheel wheel = new Wheel(otherCar.getWheel());
         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;

  /*Simple Constructor */
  Wheel(String company){
    this.company = company;
  }

  /*Copy Constructor */
  Wheel(Wheel wheel){
    this.company = wheel.getCompany();
  }

  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 in java using Deep copy.
Copying Object by Copy Constructors.
Copying Object by deep copy.
Copying Object by shallow Copy.

No comments:

Post a Comment