class

Deep Copy example

With this example we are going to demonstrate how to create a deep copy of a class. In short, to create a deep copy of a class:

  • We have created a class, Address that implements the Cloneable interface and in its overriden clone() method it uses its superclass clone(). It has String fields that uses in its constructors. In its default constructor it sets all fields to empty Strings, in the constructor using fields it initializes all fields to the given ones. It also has a method, getAddr() that returns a String with the values of its fields.
  • We have also created a class, Employee that also implementes the Cloneable interface, with two String fields a Double field and a field that is reference to Address. In its overriden clone() method it calls its superclass clone() to get a clone object of Employee, and also calls clone() method of Address to get a clone of Address object to set it to the Address field of Employee. It also has getters and setters for its fields.
  • We create a new Employee object, and initialize its fields.
  • Then we get a clone of this object, using the clone() method of Employee.
  • We use a method void printEmployee(Employee e), that prints the values of Employee, to get the values of both objects.
  • Then, we change a field of the clone object and print the two objects again. The clone object is changed but the original object is not.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

public class DeepCopy {

    public static void main(String[] args) {

  

  Employee employee1 = new Employee("M", "A");

  employee1.setValue(40000.0);

  employee1.addr = new Address("First Street", "San F", "CA", "93702");

  Employee employee2 = (Employee) employee1.clone();

  printEmployee(employee1);

  printEmployee(employee2);


  employee2.setLN("Smith");

  employee2.addr = new Address("Street", "B", "CA", "93722");


  printEmployee(employee1);

  printEmployee(employee2);

    }

    private static void printEmployee(Employee e) {

  System.out.println(e.getFN() + " " + e.getLN());

  System.out.println(e.addr.getAddr());

  System.out.println("Salary: " + e.getValue());
    }
}
class Employee implements Cloneable {

    private String lN;
    private String fN;
    private Double value;
    public Address addr;

    public Employee(String lastName, String firstName) {

  

  this.lN = lastName;

  this.fN = firstName;

  this.addr = new Address();
    }

    public String getLN() {

  return this.lN;
    }

    public void setLN(String lastName) {

  this.lN = lastName;
    }

    public String getFN() {

  return this.fN;
    }

    public void setFirstName(String firstName) {

  this.fN = firstName;
    }

    public Double getValue() {

  return this.value;
    }

    public void setValue(Double salary) {

  this.value = salary;

    }

    @Override
    public Object clone() {

  Employee emp;

  try {


emp = (Employee) super.clone();


emp.addr = (Address) addr.clone();

  } catch (CloneNotSupportedException e) {


return null; // will never happen

  }

  return emp;
    }

    @Override
    public String toString() {

  return this.getClass().getName() + "[" + this.fN + " " + this.lN + ", "


    + this.value + "]";
    }
}

class Address implements Cloneable {

    private String street;
    private String city;
    private String state;
    private String zipCode;

    public Address() {

  this.street = "";

  this.city = "";

  this.state = "";

  this.zipCode = "";
    }

    public Address(String street, String city, String state, String zipCode) {

  this.street = street;

  this.city = city;

  this.state = state;

  this.zipCode = zipCode;
    }

    @Override
    public Object clone() {

  try {


return super.clone();

  } catch (CloneNotSupportedException e) {


return null; // will never happen

  }
    }

    public String getAddr() {

  return this.street + "n" + this.city + ", " + this.state + " " + this.zipCode;
    }
}

Output:

A M
First Street
San F, CA 93702
Salary: 40000.0
A M
First Street
San F, CA 93702
Salary: 40000.0
A M
First Street
San F, CA 93702
Salary: 40000.0
A Smith
Street
B, CA 93722
Salary: 40000.0

 
This was an example of how to create a deep copy of a class in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button