CS 2114 Chapter Notes - Chapter Cloning (Java Interlude): Object Copying, Cloning, Immutable Object
![](https://new-preview-html.oneclass.com/pl62Dqo4LMV8QqJY1V0ymwPeyEX1aYOR/bg1.png)
Daniel T. Eisert CS-2114
1
J.I. 9 – Cloning
Reading XII
CS-2114: Software Design & Data Structures
Cloneable Objects
Clones are a copy of an object. Typically, only mutable objects are cloned. Since
sharing an immutable object is safe, cloning it is usually unnecessary.
- The class Object contains a protected method clone that returns a copy of an
object. The method has the heading:
protected Object clone() throws CloneNotSupportedException
- Since clone is a protected method and Object is the superclass of all other
classes, the implementation of any method can contain the invocation:
super.clone()
- Clients of a class cannot invoke clone unless the class override clone unless
the class overrides it and declares it public. However, making copies of
object can be expensive, so it might be something you do not want a class to
do.
- If you want a class to contain a public method clone, the class needs to state
this fact by implementing the Java interface Cloneable which is in the
java.lang package. The Cloneable interface declares no methods; it serves
only as a way for a class to indicate that it implements clone.
- If you forget to implement Cloneable in the class definition, and you use an
instance of your class to invoke clone, the exception
CloneNotSupportedException will occur.
• EXAMPLE: Cloning a Name object.
public class Name implements Cloneable
...
public Object clone() {
Name theCopy = null;
try {
theCopy = (Name)super.clone();
}
catch (CloneNotSupportedException e) {
“yste.err.pritlNae caot cloe: +
e.toString());
}
return theCopy;
}
- Since super.clone() returns an instance of Object, we can cast this instance to
Name. The return statement will implicitly cast theCopy to Object as
required.
- If we declared theCopy‘s datatype as Object, we could not invoke Name
methods within the clone method.
- Since a clone method was written for the class Name, the exception will
never occur.
Cloneable Objects
Two Ways to Make Copies:
find more resources at oneclass.com
find more resources at oneclass.com
![](https://new-preview-html.oneclass.com/pl62Dqo4LMV8QqJY1V0ymwPeyEX1aYOR/bg2.png)
Daniel T. Eisert CS-2114
2
Cloneable Objects
- One can copy the reference to the object and share the object with the clone.
The copy is known as a shallow copy, and the clone is called a shallow
clone ( the object and the clone both point to the same piece of data /
same reference).
- One can also copy the object itself. In this case, the copy is called a deep
copy; the clone is a deep clone ( the data / reference is copied so each
object points to its own version of the same data).
Cloning with Fields:
• EXAMPLE: Creating a shallow clone of a field. Suppose the Name
constructor takes two parameters—one for first name and another for last
name.
Nae april = ew NaeApril, Joes;
Name twin = (Name)april.clone();
...
twi.setLast“ith;
Only the last name of twin will be changed to Smith while the last name of
april will still be Jones.
• EXAMPLE: Creating a deep clone of a single field. If a class has mutable
objects as data fields, you MUST clone the objects and not simple copy
their references.
public class Student implements Cloneable {
private Name fullName;
private String id;
//Constructors and methods
...
public Object clone() {
Student theCopy = null;
try {
theCopy = (Student)super.clone();
}
catch (CloneNotSupportedException e) {
throw new Error(e.toString());
}
theCopy.fullName = (Name)fullName.clone();
If we omitted the above line, only a shallow copy would be made.
return theCopy;
}
}
• EXAMPLE: Cloning a subclass of Student, CollegeStudent.
public class CollegeStudent extends Student implements Cloneable {
private int year;
private String degree;
//constructors and methods go here
}
...
public Object clone() {
CollegeStudent theCopy =
(CollegeStudent)super.clone();
return theCopy;
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Since sharing an immutable object is safe, cloning it is usually unnecessary. The class object contains a protected method clone that returns a copy of an object. The method has the heading: protected object clone() throws clonenotsupportedexception. Since clone is a protected method and object is the superclass of all other classes, the implementation of any method can contain the invocation: super. clone() Clients of a class cannot invoke clone unless the class override clone unless the class overrides it and declares it public. However, making copies of object can be expensive, so it might be something you do not want a class to do. If you want a class to contain a public method clone, the class needs to state this fact by implementing the java interface cloneable which is in the java. lang package. The cloneable interface declares no methods; it serves only as a way for a class to indicate that it implements clone.