CodesJava

Easy learning with example program codes

java cloning deep and shallow example


Dictionary meaning of cloning.
Cloning: make an identical copy of.

Object cloning: refers to creation of exact copy of an object. Object class clone() method is used to clone an object in java.

Clone() method: Creates and returns a copy of this object.

protected Object clone() throws CloneNotSupportedException

Note: The class whose object have to be cloned must implement the java.lang.Cloneable interface otherwise clone() method will throw CloneNotSupportedException.

Why use clone() method?

The clone() methods copies the values of an object to another. The clone() method saves the extra processing task for creating the exact copy of an object.

Main disadvantage of clone() method: It does not invoke any constructor so we don’t have any control over object construction.

Types of cloning in java

  • Shallow Cloning
  • Deep Cloning

Shallow Cloning

Shallow means having little depth.

Shallow copy of an object create a new object and copies all the field of object to the new object (either it is primitive or a reference). In case of non-primitive types only reference will be copied to the new instance. Therefore both original object and new object will point to the same non-primitive type fields i.e. objects. It returns an instance of Object class we have to explicitly cast to the original object.

Let’s consider the below example, we have a Customer class which have two properties name and address. In case of shallow copy a new Customer object will be created and as both name, address fields are of non-primitive type there references will be copied to new object. Now both the original object and copied object will point to the same name and address objects.

Here what is important that in case of name values will not reflect in other if we change it in one object. This is because of immutable nature of string objects.

Shallow cloning example

package com.codesjava;
 
 
class Address implements Cloneable{
	private String city;
	private String pinCode;
 
	public Address(String city, String pinCode) {
           this.city = city;
           this.pinCode = pinCode;
        }
 
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getPinCode() {
		return pinCode;
	}
	public void setPinCode(String pinCode) {
		this.pinCode = pinCode;
	} 
	public Object clone()throws CloneNotSupportedException{  
		return super.clone();  
	}
}
 
class Customer implements Cloneable {
         private String name;
         private Address address;
         public Customer(String name, Address address) {
            this.name = name;
            this.address = address;
        }
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Object clone()throws CloneNotSupportedException{  
		return super.clone();  
	}  
}
 
public class Test {
	public static void main(String args[]){
		try{  
			Customer originalObj = new Customer("Vivek", new Address("Gurgaon", "122001"));   
			Customer clonedObj=(Customer)originalObj.clone();  
			System.out.println("Original Object properties: ");  
			System.out.println(originalObj.getName());  
			System.out.println(originalObj.getAddress().getCity());  
			System.out.println(originalObj.getAddress().getPinCode());  
			System.out.println("Cloned Object properties: ");  
			System.out.println(clonedObj.getName());  
			System.out.println(clonedObj.getAddress().getCity());  
			System.out.println(clonedObj.getAddress().getPinCode());
 
			//Change in cloned object
			clonedObj.getAddress().setPinCode("122002");
 
			//Changes will reflect in original object also
			System.out.println("Original Object pincode after changes in cloned object: ");  
			System.out.println(originalObj.getAddress().getPinCode()); 
 
		}catch(CloneNotSupportedException c){
 
		}  		  
	}  		
}

Output

Original Object properties: 
Vivek
Gurgaon
122001
Cloned Object properties: 
Vivek
Gurgaon
122001
Original Object pincode after changes in cloned object: 
122002

Deep Clonnging

Deep copy of an object create a fully independent copy of an object. In case of deep copy everything will be copied to the new object. After deep copy, original object and copied object will refers to the different primitive and non-primitive fields. Changes on one object’s fields will not reflects in other’s.

Let’s consider the same example which we discussed earlier. We have a Customer class which have two properties name and address. In case of deep copy a new independent Customer object will be created. Now the original object and copied object will point to the different name and address objects.

Here what is important that in case of name values will not reflect in other if we change it in one object. This is because of immutable nature of string objects. So we do not have to do explicit cloning for that.

Deep cloning example

package com.codesjava;
 
 
class Address implements Cloneable{
	private String city;
	private String pinCode;
 
	public Address(String city, String pinCode) {
          this.city = city;
          this.pinCode = pinCode;
        }
 
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getPinCode() {
		return pinCode;
	}
	public void setPinCode(String pinCode) {
		this.pinCode = pinCode;
	} 
	public Object clone()throws CloneNotSupportedException{  
		return super.clone();  
	}
}
 
class Customer implements Cloneable {
       private String name;
       private Address address;
       public Customer(String name, Address address) {
             this.name = name;
             this.address = address;
        }
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Object clone()throws CloneNotSupportedException{  
		Customer customer = (Customer) super.clone(); 
		customer.address = (Address)this.address.clone();
		return customer;
	}  
}
 
public class Test {
	public static void main(String args[]){
		try{  
			Customer originalObj = new Customer("Vivek", new Address("Gurgaon", "122001")); 		  
			Customer clonedObj=(Customer)originalObj.clone(); 
			System.out.println("Original Object properties: "); 
			System.out.println(originalObj.getName());  
			System.out.println(originalObj.getAddress().getCity());  
			System.out.println(originalObj.getAddress().getPinCode());  
			System.out.println("Cloned Object properties: ");  
			System.out.println(clonedObj.getName());  
			System.out.println(clonedObj.getAddress().getCity());  
			System.out.println(clonedObj.getAddress().getPinCode());
 
			//Change in cloned object
			clonedObj.getAddress().setPinCode("122002");
 
			//Changes will not reflect in original object also
			System.out.println("Original Object pincode after changes in cloned object: "); 
			System.out.println(originalObj.getAddress().getPinCode()); 
 
		}catch(CloneNotSupportedException c){
 
		}  		  
	}  		
}

Output

Original Object properties: 
Vivek
Gurgaon
122001
Cloned Object properties: 
Vivek
Gurgaon
122001
Original Object pincode after changes in cloned object: 
122001

Note:

  • clone() method of the object class support shallow copy of the object.
  • In case of deep copy we have to override the clone() method of the object class and we also need to ensure that all the member class also implement the Cloneable interface.
  • There is no difference between shallow and deep copy in java if only primitive type fields or Immutable objects are there as fields in the original object.
Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap