Java Reference
In-Depth Information
. . .
public boolean equals(Object otherObject)
{
if (!super.equals(otherObject)) return
false;
CollectibleCoin other = (CollectibleCoin)
otherObject;
return year == other.year;
}
private int year;
}
10.8.3 The clone Method
You know that copying an object reference simply gives you two references to the
same object:
BankAccount account = new BankAccount(1000);
BankAccount account2 = account;
account2.deposit(500);
// Now both account and account2 refer to a bank
account with a balance of 1500
What can you do if you actually want to make a copy of an object? That is the
purpose of the clone method. The clone method must return a new object that
has an identical state to the existing object (see Figure 11 ).
The clone method makes a new object with the same state as an existing object.
Figure 11
Cloning Objects
471
Search WWH ::




Custom Search