Monday, 18 November 2019

SE - 40 - Copy Constructor in Java

We can create a copy constructor by giving the parameter as object of same class . Once created then we copy each field  of the input object into the new instance

Note :
1) Java never creates a copy constructor by default rather we need to manually create it.
2) Copy constructor and cloning in Java are not same

public class Student {
    private int roll;
    private String name;
     
    public Employee(Student stu) {
        this.id = stu.roll;
        this.name = stu.name;
    }
}



No comments:

Post a Comment