Copy constructor in java is one of ways by which we can make copy of an object. Copy Constructor in java is just like other constructors in a class but it accepts only single argument and type of that argument is same as to which this constructor belongs to.
Lets see its examples.
Copy constructor in java using Deep copy
This example shows how to make copy of an object using copy constructor implemented with deep copy. Here Car class contains an object of type Wheel as its component.
Wheel.java
Car.java
Output:
Car1 description:
Year:2011
Make:Toyota
Wheel Description:
Company:Ceat
Car2 description(Copy of car1):
Year:2011
Make:Toyota
Wheel Description:
Company:Ceat
A seen from the output that both cars are copy of each other and have same values of their attributes.
Copy constructor in java using Copy Constructor
In above example to copy the Wheel object within Car we have used deep copy. But we can also make a copy constructor in Wheel class as we made in Car class. This copy constructor of Wheel can then be used in Car's copy constructor instead of using deep copy.
Example:
Wheel.java
Car.java
Output:
Car1 description:
Year:2011
Make:Toyota
Wheel Description:
Company:Ceat
Car2 description(Copy of car1):
Year:2011
Make:Toyota
Wheel Description:
Company:Ceat
Lets see its examples.
Copy constructor in java using Deep copy
This example shows how to make copy of an object using copy constructor implemented with deep copy. Here Car class contains an object of type Wheel as its component.
Wheel.java
class Wheel{
private String company;
Wheel(String company){
this.company = company;
}
public void setCompany(String company){
this.company=company;
}
public String getCompany(){
return company;
}
}
private String company;
Wheel(String company){
this.company = company;
}
public void setCompany(String company){
this.company=company;
}
public String getCompany(){
return company;
}
}
Car.java
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();
/*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());
}
}
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();
/*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());
}
}
Output:
Car1 description:
Year:2011
Make:Toyota
Wheel Description:
Company:Ceat
Car2 description(Copy of car1):
Year:2011
Make:Toyota
Wheel Description:
Company:Ceat
A seen from the output that both cars are copy of each other and have same values of their attributes.
Copy constructor in java using Copy Constructor
In above example to copy the Wheel object within Car we have used deep copy. But we can also make a copy constructor in Wheel class as we made in Car class. This copy constructor of Wheel can then be used in Car's copy constructor instead of using deep copy.
Example:
Wheel.java
class Wheel{
private String company;
/*Simple Constructor */
Wheel(String company){
this.company = company;
}
this.company = wheel.getCompany();
}
public void setCompany(String company){
this.company=company;
}
public String getCompany(){
return company;
}
}
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;
}
}
Car.java
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());
}
}
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());
}
}
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 like to read:
How to write a Copy Constructor using Copy Constructor.
How to write a Copy Constructor using Deep copy.
Copying Object by shallow Copy.
Copying Object by deep copy.
No comments:
Post a Comment