Object cloning is nothing but creating the extra copy of the object. The clone( ) method generates a duplicate copy of the object on which it is called. Only classes that implement the Cloneable interface can be cloned, if not CloneNotSupportedException is thrown.
Sample program for Cloning a object.
Pojo Class.
package com.searchendeca.sample;
public class Employee implements Cloneable {
String empId;
public Employee(String empId, String empName) {
super();
this.empId = empId;
this.empName = empName;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + "]";
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
String empName;
protected Employee clone() throws CloneNotSupportedException {
return (Employee) super.clone();
}
}
Main Class.
package com.searchendeca.sample;
public class EmployeeMain {
public static void main(String[] args) {
Employee emp1 = new Employee("1001", "Syed Ghouse Habib");
Employee emp2;
try {
emp2=emp1.clone();
emp1.setEmpName("Arun");
System.out.println(emp2);
System.out.println(emp1);
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output::
Employee [empId=1001, empName=Syed Ghouse Habib]
Employee [empId=1001, empName=Arun]
Now Copying can be differentiated in to two types.
Shallow copy.
It will copy only the main objects, other copies it will have the refrence of it . If one object is updated then both will get updated. Both objects are dependent. The above program given was the example of the shallow copy.
Deep Copy.
This will copy all the objects and create an exact copy of the Object given.Both are independent.We have make it as deep copy.
Pojo Class Employee
package com.searchendeca.sample;
public class Employee implements Cloneable {
Course course;
public Employee(Course course, String empId, String empName) {
super();
this.course = course;
this.empId = empId;
this.empName = empName;
}
@Override
public String toString() {
return "Employee [course=" + course + ", empId=" + empId + ", empName=" + empName + "]";
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
String empId;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
String empName;
@Override
protected Object clone() throws CloneNotSupportedException {
Employee employee=(Employee) super.clone();
employee.setCourse((Course)employee.getCourse().clone());
return employee;
}
}
Course Pojo class
package com.searchendeca.sample;
public class Course implements Cloneable {
private String courseId;
public String getCourseId() {
return courseId;
}
@Override
public String toString() {
return "Course [courseId=" + courseId + ", courseName=" + courseName + "]";
}
public Course(String courseId, String courseName) {
super();
this.courseId = courseId;
this.courseName = courseName;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
private String courseName;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Main class.
package com.searchendeca.sample;
public class EmployeeMain {
public static void main(String[] args) {
Course cos = new Course("x1", "CS");
Employee emp1 = new Employee(cos,"1001", "Syed Ghouse Habib");
try {
Employee emp2=(Employee) emp1.clone();
cos.setCourseId("y1");
System.out.println(emp2);
System.out.println(emp1);
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output::
Employee [course=Course [courseId=x1, courseName=CS], empId=1001, empName=Syed Ghouse Habib]
Employee [course=Course [courseId=y1, courseName=CS], empId=1001, empName=Syed Ghouse Habib]
From the above program we have made the clone of both Employee and course from the method hence it is Deep copy.
No comments:
Write comments