Object Cloning
Object Cloning
class Student18 implements Cloneable
{
int rollno;
String name;
Student18(int rollno,String name)
{
this.rollno=rollno;
this.name=name;
}
public Object clone()throws CloneNotSupportedException
{
return super.clone();
}
CONTD..
public static void main(String args[])
{
Try
{
Student18 s1=new Student18(101,"amit");
Student18 s2=(Student18)s1.clone();
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
}
catch(CloneNotSupportedException c)
{
}
}
OUTPUT
Output:
101 amit
101 amit
TYPES
DEEP CLONING
SHALLOW CLONING
DEEP CLONING
As the name suggests, deep cloning means
copying everything from one object to another
object.
To achieve this, we will need to trick our clone()
method provide our own cloning strategy.
Any changes made to clone object will not be
reflected in original object or vice-versa.
EXAMPLE
class Course implements Cloneable
{
String subject1;
String subject2;
String subject3;
public Course(String sub1, String sub2, String sub3)
{
this.subject1 = sub1;
this.subject2 = sub2;
this.subject3 = sub3;
}
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
class Student implements Cloneable
{
int id;
String name;
Course course;
public Student(int id, String name, Course course)
{
this.id = id;
this.name = name;
this.course = course;
}
//Overriding clone() method to create a deep copy of an object.
protected Object clone() throws CloneNotSupportedException
{
Student student = (Student) super.clone();
student.course = (Course) course.clone();
return student;
}
}
public class DeepCopyInJava
{
public static void main(String[] args)
{
Course science = new Course("Physics", "Chemistry", "Biology");
Student student1 = new Student(111, "John", science);
Student student2 = null;
try
{
//Creating a clone of student1 and assigning it to student2
student2 = (Student) student1.clone();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
//Printing the subject3 of 'student1'
System.out.println(student1.course.subject3); //Output : Biology
//Changing the subject3 of 'student2'
student2.course.subject3 = "Maths";
//This change will not be reflected in original student 'student1'
System.out.println(student1.course.subject3); //Output : Biology
}
}
SHALLOW CLONE
The default version of clone() method creates the shallow copy
of an object.
The shallow copy of an object will have exact copy of all the
fields of original object. If original object has any references to
other objects as fields, then only references of those objects are
copied into clone object, copy of those objects are not created.
That means any changes made to those objects through clone
object will be reflected in original object or vice-versa. Shallow
copy is not 100% disjoint from original object. Shallow copy is
not 100% independent of original object.
//code illustrating shallow copy
public class Ex {
private int[] data;
// makes a shallow copy of values
public Ex(int[] values) {
data = values;
}
public void showData() {
System.out.println( Arrays.toString(data) );
}
}
public class UsesEx{
public static void main(String[] args) {
int[] vals = {3, 7, 9};
Ex e = new Ex(vals);
e.showData(); // prints out [3, 7, 9]
vals[0] = 13;
e.showData(); // prints out [13, 7, 9]
// Very confusing, because we didn't
// intentionally change anything about
// the object e refers to.
}
}