Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page

Was this helpful?

  1. 15) Cloning and Immutable Class

Untitled

Topic

Simple Cloning of objects with primitives

Shallow

Deep Object inside Object

Cloning with inheritance (Shallow)

Cloning with inheritance (Deep)

Serialization

Apache commons util

Copy constructor

Cloning objects with collections

Copy Constructor

Best practices

Object cloning refers to creation of exact copy of an object. It creates a new instance of the class of current object and initializes all its fields with exactly the contents of the corresponding fields of this object.

Using Assignment Operator to create copy of reference variable In Java, there is no operator to create copy of an object. Unlike C++, in Java, if we use assignment operator then it will create a copy of reference variable and not the object. This can be explained by taking an example. Following program demonstrates the same.

package com.gs.corejava.collections;

//Java program to demonstrate that assignment 
//operator only creates a new reference to same 
//object. 

//A test class whose objects are cloned 
class Test {
	int x, y;

	Test() {
		x = 10;
		y = 20;
	}
}

// Driver Class
public class Main {
	public static void main(String[] args) {
		Test ob1 = new Test();

		System.out.println(ob1.x + " " + ob1.y);

		// Creating a new reference variable ob2
		// pointing to same address as ob1
		Test ob2 = ob1;

		// Any change made in ob2 will be reflected
		// in ob1
		ob2.x = 100;

		System.out.println(ob1.x + " " + ob1.y);
		System.out.println(ob2.x + " " + ob2.y);
	}
}

Output:

10 20
100 20
100 20

Creating a copy using clone() method

The class whose object’s copy is to be made must have a public clone method in it or in one of its parent class.

  • Every class that implements clone() should call super.clone() to obtain the cloned object reference.

  • The class must also implement java.lang.Cloneable interface whose object clone we want to create otherwise it will throw CloneNotSupportedException when clone method is called on that class’s object.

  • Syntax:

      protected Object clone() throws CloneNotSupportedException
package com.gs.corejava.collections;

//Java program to demonstrate that assignment 
//operator only creates a new reference to same 
//object. 

//A test class whose objects are cloned 
class Test implements Cloneable {
	int x, y;

	Test() {
		x = 10;
		y = 20;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

// Driver Class
public class Main {
	public static void main(String[] args) {
		Test ob1 = new Test();

		System.out.println(ob1.x + " " + ob1.y);

		// Creating a new reference variable ob2
		// pointing to same address as ob1
		Test ob2 = null;
		try {
			ob2 = (Test) ob1.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Any change made in ob2 will be reflected
		// in ob1
		ob2.x = 100;

		System.out.println(ob1.x + " " + ob1.y);
		System.out.println(ob2.x + " " + ob2.y);
	}
}

Output:

10 20
10 20
100 20

Usage of clone() method -Shallow Copy

package com.gs.corejava.collections;

//A Java program to demonstrate shallow copy 
//using clone() 

//An object reference of this class is 
//contained by Test2 
class Test {
	int x, y;
}

// Contains a reference of Test and implements
// clone with shallow copy.
class Test2 implements Cloneable {
	int a;
	int b;
	Test c = new Test();

	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

// Driver class
public class Main {
	public static void main(String args[]) throws CloneNotSupportedException {
		Test2 t1 = new Test2();
		t1.a = 10;
		t1.b = 20;
		t1.c.x = 30;
		t1.c.y = 40;

		// Creating a copy of object t1 and passing it to t2
		Test2 t2 = (Test2) t1.clone(); 
		
		// Change in primitive type of t2 will not  be reflected in t1 field
		t2.a = 100;

		// Change in object type field will be reflected in both t2 and t1(shallow copy)
		t2.c.x = 300;

		System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y);
		System.out.println(t2.a + " " + t2.b + " " + t2.c.x + " " + t2.c.y);
	}
}

Output:

10 20 300 40
100 20 300 40

In the above example, t1.clone returns the shallow copy of the object t1. To obtain a deep copy of the object certain modifications have to be made in clone method after obtaining the copy.

Deep Copy vs Shallow Copy

  • Shallow copy is method of copying an object and is followed by default in cloning. In this method the fields of an old object X are copied to the new object Y. While copying the object type field the reference is copied to Y i.e object Y will point to same location as pointed out by X. If the field value is a primitive type it copies the value of the primitive type.

  • Therefore, any changes made in referenced objects in object X or Y will be reflected in other object.

Shallow copies are cheap and simple to make. In above example, we created a shallow copy of object.

Usage of clone() method – Deep Copy

  • If we want to create a deep copy of object X and place it in a new object Y then new copy of any referenced objects fields are created and these references are placed in object Y. This means any changes made in referenced object fields in object X or Y will be reflected only in that object and not in the other. In below example, we create a deep copy of object.

  • A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.

package com.gs.corejava.collections;

//A Java program to demonstrate deep copy 
//using clone() 

//An object reference of this class is 
//contained by Test2 
class Test {
	int x, y;
}

// Contains a reference of Test and implements
// clone with deep copy.
class Test2 implements Cloneable {
	int a, b;

	Test c = new Test();

	public Object clone() throws CloneNotSupportedException {
		// Assign the shallow copy to new refernce variable t
		Test2 t = (Test2) super.clone();

		// Create a new object for the field c
		// and assign it to shallow copy obtained,
		// to make it a deep copy
		t.c = new Test();

		return t;
	}
}

public class Main {
	public static void main(String args[]) throws CloneNotSupportedException {
		Test2 t1 = new Test2();
		t1.a = 10;
		t1.b = 20;
		t1.c.x = 30;
		t1.c.y = 40;

		Test2 t3 = (Test2) t1.clone();
		// Change in primitive type of t2 will not
		// be reflected in t1 field
		t3.a = 100;


		// Change in object type field of t2 will not
		// be reflected in t1(deep copy)
		t3.c.x = 300;

		System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y);
		System.out.println(t3.a + " " + t3.b + " " + t3.c.x + " " + t3.c.y);
	}
}

Output:

10 20 30 40
100 20 300 0

Cloning with inheritance (Shallow) :

Example 1:

package com.gs.corejava.collections;

//: appendixa:HorrorFlick.java
//You can insert Cloneability at any level of inheritance.
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.

class Person {
}

class Hero extends Person {
}

class Scientist extends Person implements Cloneable {
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			// This should never happen: It's Cloneable already!
			throw new RuntimeException(e);
		}
	}
}

class MadScientist extends Scientist {
}

public class HorrorFlick {
	public static void main(String[] args) {
		Person p = new Person();
		Hero h = new Hero();
		Scientist s = new Scientist();
		MadScientist m = new MadScientist();
		// ! p = (Person)p.clone(); // Compile error
		// ! h = (Hero)h.clone(); // Compile error
		s = (Scientist) s.clone();
		System.out.println(m.clone().getClass());
		m = (MadScientist) m.clone();
	}
} // /:~

Example 2:

package com.gs.corejava.collections;

class A implements Cloneable {
	@Override
	protected A clone() throws CloneNotSupportedException // could be public
	{
		Object clone = super.clone();
		System.out.println("Class A: " + clone.getClass()); // will print 'C'
		return (A) clone;
	}
}

class B extends A {
	@Override
	protected B clone() throws CloneNotSupportedException {
		A clone = super.clone();
		System.out.println("Class B: " + clone.getClass()); // will print 'C'
		return (B) clone;
	}
}

class C extends B {
	@Override
	protected C clone() throws CloneNotSupportedException {
		B clone = super.clone();
		System.out.println("Class C: " + clone.getClass()); // will print 'C'
		return (C) clone;
	}
}

public class TestI {

	public static void main(String argv[]) {
		C c = new C();
		try {
			A cloned_c = c.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:

Class A: class com.gs.corejava.collections.C
Class B: class com.gs.corejava.collections.C
Class C: class com.gs.corejava.collections.C
Previous16) Serialization And DeserializationNextJAVA 8

Last updated 6 years ago

Was this helpful?