An Introduction To Deep Cloning in Java Part 3

Posted By : Aftab Alam | 02-Jun-2020

1. Introduction

As a Java developer, it's very much important to know about deep cloning mechanism. In this blog, we are going to have a closer look at the concept of cloning in Java application development.

In Java app development services, cloning is a mechanism in which an object is created from another object without using the new operator. It has been categorized in categories.

  1. Swallow Cloning

  2. Deep Cloning

 

2. Requirements

Have a look at the pre-requisites given below:

   2.1. Ubuntu 14.04 LTS

   2.2. Ubuntu gedit editor

   2.3. java version "1.8.0_181" 

 

3. Implementation

It has been divided into multiple steps.

   3.1. Deep Cloning:- As we have seen in case of swallow cloning, only object's primitive(Integer, Float, Double, Character, Boolean, String, etc) properties are cloned. In other words, Object's primitive data-member(s)/properties get separate memory as same as while creating an object using the new operator except reference variables(user defined classes, map, and list). Deep cloning is not directly supported by Java. For achieving deep cloning, we need to write down logic explicitly to achieve it.

  3.1.1. Student:- It has three properties out of which two are primitives and one is non-primitive(map). Map doesn't clone implicitly so we need to implement its cloning logic explicitly. Student clone method has logic to clone map in which iterator is used to achieve it.

class Student implements Cloneable {
  
   Long id;
   String name;
   Map address;

   public Student(Long id, String name, Map address) {
     this.id = id;
     this.name = name;
     this.address = address;
   }
 
   public String getName() {
      return name;
   }
  
   public Long getId() {
     return id;
   }

   public Map getAddress() {
     return address;
   }

   void setName(String name) {
      this.name = name;
   }

   void setId(Long id) {
      this.id = id;
   }

   void setAddress(Map address) {
      this.address = address;
   }

   public Student clone() {
     try {
       Student student = (Student)super.clone();
       Map address = new HashMap();
       Set keys = this.address.keySet();
       System.out.println("keys:"+keys);
       Iterator iterator = keys.iterator();
       while(iterator.hasNext()) {
         String key = (String)iterator.next();
         address.put(key, this.address.get(key));
       }
       student.address = address;
       return student;
      } catch(Exception E) {
         System.out.println("Ex: "+E.getMessage());
	return new Student(0l, "Excep", new HashMap());
      }
   }
}

3.1.2. DeepCloningDemoPartIII:-

It demostrates the cloning of Map property in a class. For achieving the same, Student class is created which has non-primitive property(Map).

public class DeepCloningDemoPartIII {

   public static void main(String a[]) {
      Map address = new HashMap();
      address.put("postal", "postal");
      address.put("address", "address");
      Student student1 = new Student(1000l, "Aftab", address);
      System.out.println("Name1: "+student1.getName());
      System.out.println("Id1: "+student1.getId());
      System.out.println("Address: "+student1.getAddress().get("postal"));
      Student student2 = student1.clone();
      System.out.println("Name1: "+student2.getName());
      System.out.println("Id2: "+student2.getId());
      System.out.println("Address: "+student2.getAddress().get("postal"));
      student1.setName("Khan");
      Map addr = student1.getAddress();
      addr.put("postal", "postal2");
      student1.setAddress(addr);
      System.out.println("Name1: "+student1.getName());
      System.out.println("Id1: "+student1.getId());
      System.out.println("Address1: "+student1.getAddress().get("postal"));
      System.out.println("Name1: "+student2.getName());
      System.out.println("Id2: "+student2.getId());   
      System.out.println("Address2: "+student2.getAddress().get("postal"));  
   }
}

3.2. Commands

To compile and execute this program, listed below commands are used on ubuntu terminal.

     3.2.1. "javac DeepCloningPartIIIDemo.java" to compile the java program

     3.2.2. "java DeepCloningPartIIIDemo" to execute the program which produces the result shown in below screenshot.

 

3.3. Output

When the above program is executed then It produces an output which is shown in the screenshot.

 

4. Conclusion

In Java application development, it is best suited when every instance of parent class wants to have a separate copy of list of its reference type variable(class). 

About Author

Author Image
Aftab Alam

Aftab has worked on multiple technologies in front-end as well as in back-end.

Request for Proposal

Name is required

Comment is required

Sending message..