class

Deep Copy example – Part 2

This is an example of how to create a deep copy of a class. In order to create a deep copy of a class we have overriden the clone() API method of Cloneable interface, as described in the following steps:

  • We have created a class, Car that has a String field and a getter and setter method for it. It also has a constructor using its String field.
  • We have also created a class, Person, that implements the Cloneable interface in order to override its clone() API method.
  • It has a Car field and a String field, getters for both fields and a setter method for the String field. It also has a constructor using both fields, where it initializes the Car field with a given String and sets the value of the String field to another given field.
  • We create a new instance of Person and using its getters we get the String field and the String field of its Car field.
  • We also create a clone object, using the clone method of Person.
  • We change the String field of clone object and the String field of Car field of the clone object. Both fields are changed in the clone object, whereas the original objects has held its initial values.

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) {

  //Original Object

  Person p = new Person("Person-A", "Civic");

  System.out.println("Original (orginal values): " + p.getName() + " - "


    + p.getCar().getName());


  //Clone as a shallow copy

  Person q = (Person) p.clone();


  System.out.println("Clone (before change): " + q.getName() + " - "


    + q.getCar().getName());


  //change the primitive member

  q.setName("Person-B");


  //change the lower-level object

  q.getCar().setName("Accord");


  System.out.println("Clone (after change): " + q.getName() + " - "


    + q.getCar().getName());


  System.out.println("Original (after clone is modified): " + p.getName()


    + " - " + p.getCar().getName());

    }
}



class Person implements Cloneable {
    //Lower-level object

    private Car car;
    private String name;

    public Car getCar() {

  return car;
    }

    public String getName() {

  return name;
    }

    public void setName(String s) {

  name = s;
    }

    public Person(String s, String t) {

  name = s;

  car = new Car(t);
    }

    @Override
    public Object clone() {

  //Deep copy

  Person p = new Person(name, car.getName());

  return p;
    }
}

class Car {

    private String name;

    public String getName() {

  return name;
    }

    public void setName(String s) {

  name = s;
    }

    public Car(String s) {

  name = s;
    }
}

Output:

Original (orginal values): Person-A - Civic
Clone (before change): Person-A - Civic
Clone (after change): Person-B - Accord
Original (after clone is modified): Person-A - Civic

  
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