0% found this document useful (0 votes)
42 views17 pages

Object Cloning

The document discusses object cloning in Java. It explains that the clone() method of the Object class is used to create an exact copy of an object. The class being cloned must implement the Cloneable interface. There are two types of cloning - deep cloning which creates a completely independent copy of the object including nested objects, and shallow cloning which only copies the object itself but nested objects are the same between the original and clone.

Uploaded by

cahcet edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views17 pages

Object Cloning

The document discusses object cloning in Java. It explains that the clone() method of the Object class is used to create an exact copy of an object. The class being cloned must implement the Cloneable interface. There are two types of cloning - deep cloning which creates a completely independent copy of the object including nested objects, and shallow cloning which only copies the object itself but nested objects are the same between the original and clone.

Uploaded by

cahcet edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

OBJECT CLONING

 The object cloning is a way to create exact copy


of an object. The clone() method of Object class
is used to clone an object.
 The java.lang.Cloneable interface must be
implemented by the class whose object clone we
want to create. If we don't implement Cloneable
interface, clone() method
generates CloneNotSupportedException.
SYNTAX
 protected Object clone() throws CloneNotSuppo
rtedException 
WHY USE CLONE() METHOD ?

 The clone() method saves the extra processing


task for creating the exact copy of an object.

 If we perform it by using the new keyword, it


will take a lot of processing time to be performed
that is why we use object cloning.
ADVANTAGE OF OBJECT CLONING

 You don't need to write lengthy and repetitive codes.


Just use an abstract class with a 4- or 5-line long
clone() method.
 It is the easiest and most efficient way for copying
objects, especially if we are applying it to an already
developed or an old project. Just define a parent class,
implement Cloneable in it, provide the definition of
the clone() method and the task will be done.
 Clone() is the fastest way to copy array.
EXAMPLE OF CLONE() METHOD (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.
     }
 }

You might also like