Shallow Copy Deep Copy Java example

Shallow Copy Deep Copy  Java example
Before knowing about Shallow Copy and Deep Copy you need to know about Clonable interface.

Clonable interface -
    Java has a clonable interface for make clone of any object.
    Clonable Interface is a marker interface.
    Clonable Interface has no method.
    Class which implements Clonable interface, JVM give the permission for making the clone of         this class.

Shallow Copy -        
    Clone can be archived by object.clone() method. It gives the Shallow copy of the object. In this process a new object is created That have all the value and instance variable. And if main object has any references to other object then the references are copied in the shallow copy.

Example -

class A implements Cloneable{
   
    A(String personName, Integer age){
        this.personName=personName;
        this.age=age;
    }
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    private String personName = null;
    private transient Integer age = null;
   
    public Integer getAge() {
        return age;
    }
    public String getPersonName() {
        return personName;
    }
}

public class Test{
    public static void main(String[] args) throws CloneNotSupportedException {
        A a = new A("Albar", new Integer(50));
        A a1 = (A)a.clone();
        System.out.println(a1.getPersonName());
    }
}

Deep Copy:-
    Deep Copy of any object can be achieved by write the object into byte stream and again deserialize it. It gives the Deep Copy of the object. Deep copy is a totally duplicate copy of an object.
And if main object has any references to other object then the complete new copies of those objects will be available  in the Deep Copy.

Example to get Deep Copy -

class A implements Serializable{
   
    A(String personName, Integer age){
        this.personName=personName;
        this.age=age;
    }
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    private String personName = null;
    private transient Integer age = null;
   
    public Integer getAge() {
        return age;
    }
    public String getPersonName() {
        return personName;
    }
}

public class Test{
    public static void main(String[] args) throws CloneNotSupportedException {
        try {
            A a = new A("Albar", new Integer(50));
            A deepCopy = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            ObjectOutputStream out = new ObjectOutputStream(bos);

            out.writeObject(a);
            out.flush();
            out.close();

            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
            Object obj = in.readObject();
            deepCopy = (A)obj;
            System.out.println(deepCopy.getPersonName());
            }
            catch(IOException e) {
                e.printStackTrace();
            }
            catch(ClassNotFoundException cnfe) {
                cnfe.printStackTrace();
            }
            }
}

Comments

  1. Good explanation, It' help full.

    ReplyDelete
  2. Thanks for putting it across. Recently, I had written about how shallow and deep cloning is highlighted when not using clone method. Basically if we are using copy constructor, then also we need to make sure that the new object doesn't share member reference variables with the object being copied from.

    ReplyDelete

Post a Comment

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Use of req.query, req.params and req.body in NODE JS

How to capture finger prints in java