An Introduction To Swallow Cloning in Java

Posted By : Aftab Alam | 12-May-2020

This blog post provides an introduction to Swallow Cloning in java and why it is an essential part of Java application development. 

 

1. Introduction

As Java developers, we know about the cloning concept. In this blog, we are going to have a close look at the concept of cloning in Java application development.

In Java, 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

To execute this program, pre-requisites have been listed below out.

   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. Swallow Cloning:- In this type of cloning, object primitive(Integer, Float, Double, Character, Boolean, String, etc) properties are cloned. It means that they get separate memory as same as while creating an object using the new operator. For its implementation, the following classes(Student, Course, and SwallowCloningDemo) have been created and accordingly, their functionalities have been implemented.

 

  3.1.1. Student:- It has three properties. It implements a cloneable interface which gives the facility to clone an object by overriding the clone method. Id and name would be cloned directly while student instance is cloned but Course instance will not be cloned in swallow cloning because it's a reference type.

class Student implements Cloneable {
  
   Long id;
   String name;
   Course course;

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

   public Course getCourse() {
     return course;
   }

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

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

   void setCourse(Course course) {
      this.course = course;
   }

   public Student clone() {
     try {
       Student student = (Student)super.clone();
       return student;
      } catch(Exception E) {
         System.out.println("Ex: "+E.getMessage());
	return new Student(0l, "Excep", new Course(10l, "ccc"));
      }
   }
}

   3.1.2. Course:- It has two data members id, and name. It doesn't implement.

 

class Course {
  
   Long id;
   String name;

   public Course(Long id, String name) {
      this.id = id;
      this.name = name;
   }

   public String getName() {
      return name;
   }
  
   public Long getId() {
     return id;
   }

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

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

   3.1.3. SwallowCloningDemo:- It demontrates swallow cloning.

public class SwallowCloningDemo {

   public static void main(String a[]) {
      Course course = new Course(1l, "Java");
      Student student1 = new Student(1000l, "Aftab", course);
      Student student2 = student1.clone();
      System.out.println("Name1: "+student1.getName());
      System.out.println("Id1: "+student1.getId());
      System.out.println("Course Name1: "+student1.getCourse().getName());
      System.out.println("Name1: "+student2.getName());
      System.out.println("Id2: "+student2.getId());
      System.out.println("Course Name2: "+student2.getCourse().getName());
      
      student1.setName("Khan");
      student1.getCourse().setName("C++");
      System.out.println("Name1: "+student1.getName());
      System.out.println("Id1: "+student1.getId());
      System.out.println("Course Name1: "+student1.getCourse().getName());
      System.out.println("Name1: "+student2.getName());
      System.out.println("Id2: "+student2.getId());  
      System.out.println("Course Name2: "+student2.getCourse().getName());  
   }
}

3.2. Commands

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

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

     3.2.2. "java SwallowCloningDemo" 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.

3.4. References

Listed below out are some references.

 3.4.1. As per my understanding from multiple sources.

 

4. Conclusion

It's best suitable in many situations.

4.1. It is the best where we need parent multiple instances but wants to have only single instance for its reference data member.

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..