Although cloning methods are defined in the most basic object of all classes, clones are not automatically performed in each class. This may seem strange, because the underlying class method is certainly available in derivative classes. But Java does seem to do the opposite; If you want to use a clone method in a class, the only way to do that is to add some code specifically to ensure that the clone is working properly.
1. Techniques for using protected
The Clone () method is "reserved" (set to protected) in the underlying class object to avoid the default cloning capability for each class that we create. The result: For the client programmer who simply uses the class, they do not have this method by default, and secondly, we cannot invoke clone () with a handle to the underlying class (although that would be particularly useful in some cases, such as cloning a series of objects in a pleomorphic way). At compile time, this is actually a way of informing us that the object is not cloned-and, most strangely, most of the classes in the Java library cannot be cloned. So, if we execute the following code:
Integer x = new Integer (l);
x = X.clone ();
then, at compile time, there's a nasty error message that pops up and tells us that you can't access clone ()-- Because the integer does not overwrite it, and it is the default for the protected version.
However, if we are in a class derived from object (all classes are derived from object), we have the right to invoke Object.clone () because it is "protected" and we are in an inheritance. The base class Clone () provides a useful feature-it does a real "bitwise" Copy of the derived class object, so it is equivalent to the standard cloning action. However, we then need to set our own clone operations public, otherwise we will not be able to access them. All in all, the two key issues to be aware of when cloning are: almost certain to call Super.clone (), and note that the clone is set to public. The
sometimes wants to overwrite clone () in a deeper derivative class, or use our Clone () (now public), which is not necessarily what we want (however, since Object.clone () has produced a copy of the actual object, So it's possible to allow that too. Protected's technique can only be used once: it inherits from a class that does not have cloning capability for the first time, and wants to make a class "capable of cloning". In any case inherited from our class, The Clone () method is available, because it is not possible for Java to narrow the scope of the method after derivation. In other words, once an object becomes capable of cloning, anything derived from it can be cloned, unless a special mechanism (discussed later) is used to "close" the cloning capability.
2. Implement Cloneable interface
To make the clone of an object work well, another thing needs to be done: Implement the Cloneable interface. This interface makes people feel a little strange because it is empty!
Interface Cloneable {}
The reason to implement this empty interface is obviously not because we are prepared to trace back into a cloneable, and a method of calling it. Some people think that using an interface here is a kind of "cheating" behavior because the features it uses are other ideas rather than the original meaning. The implementation of the Cloneable interface acts as a tagged role, encapsulated into the class type.
Two reasons contributed to the existence of cloneable interface. First of all, there may be a back-styling handle that points to an underlying type and does not know if it really can clone that object. In this case, you can use the instanceof keyword (described in chapter 11th) to investigate whether a handle is actually connected to an object that can be cloned:
if (myhandle instanceof cloneable)//...
The second reason is that we may not want all object types to be cloned. So Object.clone () verifies that a class really implements the Cloneable interface. If the answer is no, then "throw" a clonenotsupportedexception offence. So in general, we have to put "implement cloneable" as part of the support for cloning capabilities.