Java Reference
In-Depth Information
EXAMPLE:
(continued)
If you look only quickly at Display 15.14 you might think the following at the start
of the definition is an unimportant detail:
implements
PubliclyCloneable
However, it ensures that the linked list class implements the
Cloneable
interface. In
order for a class to have a Java-approved
clone
method, it must implement the
Clone-
able
interface. It also allows you to make linked lists of linked lists and have a deep
copy
clone
method in the linked list of linked lists.
A sample class that implements the
PubliclyCloneable
interface is given in Dis-
play 15.15. Display 15.16 shows a demonstration program that makes a deep copy
clone
of a linked list of objects of this sample class.
Display 15.15
A
PubliclyCloneable
Class
(part 1 of 2)
1
public
class
StockItem
implements
PubliclyCloneable
2{
3
private
String name;
4
private
int
number;
5
public
StockItem( )
6
{
7
name =
null
;
8
number = 0;
9
}
10
public
StockItem(String nameData,
int
numberData)
11
{
12
name = nameData;
13
number = numberData;
14
}
15
public
void
setNumber(
int
newNumber)
16
{
17
number = newNumber;
18
}
(continued)