Java Reference
In-Depth Information
cloned object. Here is a first attempt to implement the clone method for the
BankAccount class:
public class BankAccount
{
. . .
public Object clone()
{
// Not complete
Object clonedAccount = super.clone();
return clonedAccount;
}
}
However, this Object.clone method must be used with care. It only shifts
the problem of cloning by one level; it does not completely solve it. Specifically,
if an object contains a reference to another object, then the Object.clone
method makes a copy of that object reference, not a clone of that object. The
figure below shows how the Object.clone method works with a Customer
object that has references to a String object and a BankAccount object. As
you can see, the Object.clone method copies the references to the cloned
Customer object and does not clone the objects to which they refer. Such a
copy is called a shallow copy.
473
474
The Object.clone Method Makes a Shallow Copy
Search WWH ::




Custom Search