Chapter 6
Chapter 6
Copy
original =
Employee
copy =
Cloning
original =
Employee
copy =
Employee
Types of Cloning
Two types of copying an object
1. Shallow copy
2. Deep copy
Shallow Copy
Shallow copy is a bit-wise copy of an object. A new object is created that has an
exact copy of the values in the original object. If any of the fields of the object are
references to other objects, just the reference addresses are copied i.e., only the memory
address is copied.
In this figure 6.4, the MainObject1 have fields "field1" of int type, and
"ContainObject1" of ContainObject type. When you do a deep copy of MainObject1,
MainObject2 is created with "field3" containing the copied value of "field1" and
"ContainObject2" containing the copied value of ContainObject1.So any changes made
to ContainObject1 in MainObject1 will not reflect in MainObject2.
Shallow copy
class WrittenBy
{
private String name;
public String getName()
{
return name;
}
public void setName(String s)
{
name = s;
}
public WrittenBy(String s)
{
name = s;
}
}
class Project implements Cloneable
{
//Contained object
private WrittenBy write;
private String name;
public WrittenBy getWrite()
{
return write;
}
public String getName()
{
return name;
}
public void setName(String s)
{
name = s;
}
public Project(String s, String pro)
{
6.4 Java Programming Paradigms
name = s;
write = new WrittenBy(pro);
}
public Object clone()
{
//shallow copy
try
{
return super.clone();
}
catch (CloneNotSupportedException e)
{
return null;
}
}
}
public class ShallowCopy
{
public static void main(String[] args)
{
//Original Object
Project write = new Project("OAK","james");
System.out.println("Original Object: " + write.getName() + " - "+ write.getWrite().getName());
//Clone Object
Project clonedproj = (Project) write.clone();
System.out.println("Cloned Object:" + clonedproj.getName() + " - "+
clonedproj.getWrite().getName());
write.setName("Java");
write.getWrite().setName("James Gosling");
From the above example, implement the class that you want to copy with
Clonable interface and override clone() method of Object class and call super.clone() in
it. If you observe, the changes made to "name" field of original object (Project class) is
not reflected in cloned object but the changes made to "name" field of contained object
(WrittenBy class) is reflected in cloned object. This is because the cloned object carries
the memory address of the WrittenBy object but not the actual values. Hence any updates
on the WrittenBy object in Original object will reflect in Cloned object.
Deep copy
class WrittenBy
{
private String name;
public String getName()
{
return name;
}
public void setName(String s)
{
name = s;
}
public WrittenBy(String s)
{
name = s;
}
}
class Project implements Cloneable
{
//Contained object
private WrittenBy write;
private String name;
public WrittenBy getWrite()
{
return write;
}
public String getName()
{
return name;
6.6 Java Programming Paradigms
}
public void setName(String s)
{
name = s;
}
public Project(String s, String pro)
{
name = s;
write = new WrittenBy(pro);
}
public Object clone()
{
//Deep Copy
Project p=new Project(name, write.getName());
return p;
}
}
public class DeepCopy
{
public static void main(String[] args)
{
//Original Object
Project write = new Project("OAK","james");
System.out.println("Original Object: " + write.getName() + " - "+
write.getWrite().getName());
//Clone Object
Project clonedproj = (Project) write.clone();
System.out.println("Cloned Object:" + clonedproj.getName() + " -
"+clonedproj.getWrite().getName());
write.setName("Java");
write.getWrite().setName("James Gosling");
Output
I:\java\OOPS>javac DeepCopy.java
I:\java\OOPS>java DeepCopy
Original Object: OAK - james
Cloned Object:OAK - james
Original Object after it is updated: Java - James Gosling
Cloned Object after updating original object: OAK - james
Notice that in the "Project" class, you will see only the change in the "clone()" method.
Since its a deep copy, you need to create an object of the cloned class.
All of the Class objects in the interfaces array must represent interfaces, not
classes or primitive types.
No two elements in the interfaces array may refer to identical Class objects.
All of the interface types must be visible by name through the specified class loader.
In other words, for class loader cl and every interface i, the following expression
must be true:
Class.forName(i.getName(), false, cl) == i
All non-public interfaces must be in the same package; otherwise, it would not be
possible for the proxy class to implement all of the interfaces, regardless of what
package it is defined in.
For any set of member methods of the specified interfaces that have the same
signature:
o If the return type of any of the methods is a primitive type or void, then all of
the methods must have that same return type.
o Otherwise, one of the methods must have a return type that is assignable to all
of the return types of the rest of the methods.
The resulting proxy class must not exceed any limits imposed on classes by the
virtual machine. For example, the VM may limit the number of interfaces that a
class may implement to 65535; in that case, the size of the interfaces array
must not exceed 65535.
already defined in the same class loader and the same package with particular
signers.
Since a proxy class implements all of the interfaces specified at its creation,
invoking getInterfaces on its Class object will return an array containing the same
list of interfaces (in the order specified at its creation), invoking getMethods on its
Class object will return an array of Method objects that include all of the methods
in those interfaces, and invoking getMethod will find methods in the proxy
interfaces as would be expected.
The Proxy.isProxyClass method will return true if it is passed a proxy class-- a
class returned by Proxy.getProxyClass or the class of an object returned by
Proxy.newProxyInstance-- and false otherwise.
The java.security.ProtectionDomain of a proxy class is the same as that of system
classes loaded by the bootstrap class loader, such as java.lang.Object, because the
code for a proxy class is generated by trusted system code. This protection
domain will typically be granted java.security.AllPermission.
Review Questions
Part-A
1) Differentiate between copying and cloning.
2) What is shallow copy?
3) What is deep copy?
4) Define proxies.
5) What are the properties of proxies classes.
Part- B
1) Describe cloning in details.
2) Describe shallow copy and deep copy with example program.
3) Describe proxy classes.