14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

clone (Java method) 54<br />

clone (Java method)<br />

clone() is a method in the Java programming language for object duplication. In Java, objects are manipulated<br />

through reference variables, and there is no operator for copying an object -- the assignment operator duplicates the<br />

reference, not the object. The clone() method provides this functionality.<br />

Overview<br />

Classes that want copying functionality must implement some method to do so. To a certain extent that function is<br />

provided by "clone()".<br />

clone() acts like a constructor. Typically it calls the clone() method of its superclass to obtain the copy, etc. until it<br />

eventually reaches Object's clone() method. The special clone() method in the base class Object provides a standard<br />

mechanism for duplicating objects.<br />

The class Object's clone() method creates and returns a copy of the object, with the same class and with all the fields<br />

having the same values. However, clone() throws a CloneNotSupportedException unless the class you are trying to<br />

use it on implements the marker interface Cloneable.<br />

The default implementation of Object.clone() performs a shallow copy. When a class desires a deep copy or some<br />

other custom behavior, they must perform that in their own clone() method after they obtain the copy from the<br />

superclass.<br />

The syntax for calling clone in Java is:<br />

Object copy = obj.clone();<br />

or commonly<br />

MyClass copy = (MyClass) obj.clone();<br />

which provides the typecasting needed to assign the generic Object reference returned from clone to a reference to a<br />

MyClass object.<br />

One disadvantage with the design of the clone() method is that the return type of clone() is Object, and needs to be<br />

explicitly cast back into the appropriate type. However, overriding clone() to return the appropriate type is preferable<br />

and eliminates the need for casting in the client (using covariant return types, since J2SE 5.0).<br />

Another disadvantage is that one often cannot access the clone() method on an abstract type. Most interfaces and<br />

abstract classes in Java do not specify a public clone() method. As a result, often the only way to use the clone()<br />

method is if you know the actual class of an object; which is contrary to the abstraction principle of using the most<br />

generic type possible. For example, if one has a List reference in Java, one cannot invoke clone() on that reference<br />

because List specifies no public clone() method. Actual implementations of List like ArrayList and LinkedList all<br />

generally have clone() methods themselves, but it is inconvenient and bad abstraction to carry around the actual class<br />

type of an object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!