Java Reference
In-Depth Information
TIP: (continued)
It is now easy to defi ne our generic linked list whose clone method produces a deep
copy. The defi nition is given in Display 15.14. We have already discussed the main
points involved in this defi nition. The Programming Example subsection, “A Linked
List with a Deep Copy clone Method ,” discusses some of the minor, but possibly
unclear, details of the defi nition. 1
Display 15.13
The PubliclyCloneable Interface
1 /*
2 The programmer who defines a class implementing this interface
3 has the responsibility to define clone so it makes a deep copy
4 (in the officially sectioned way.)
5 */
6 public interface PubliclyCloneable extends Cloneable
7 {
8
public Object clone( );
9 }
Any class that implements
PubliclyCloneable
automatically implements
Cloneable .
Any class that implements
PubliclyCloneable must have a
public clone method.
Display 15.14
A Generic Linked List with a Deep Copy clone Method (part 1 of 3)
1 public class LinkedList<T extends PubliclyCloneable>
2
implements PubliclyCloneable
3 {
4 private class Node<T>
5 {
6
private T data;
7
private Node<T> link;
8 public Node( )
9 {
10 data = null ;
11 link = null ;
12 }
(continued)
1 You might wonder whether we could use a type parameter in the PubliclyCloneable interface
and so avoid some type casts in the definition copyOf . We could do that, but that may be more trou-
ble than it is worth and, at this introductory level of presentation, would be an unnecessary distraction.
 
 
Search WWH ::




Custom Search