<s>
clone( )	O
is	O
a	O
method	B-Language
in	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
for	O
object	O
duplication	O
.	O
</s>
<s>
In	O
Java	B-Language
,	O
objects	O
are	O
manipulated	O
through	O
reference	O
variables	O
,	O
and	O
there	O
is	O
no	O
operator	O
for	O
copying	O
an	O
object	O
—	O
the	O
assignment	O
operator	O
duplicates	O
the	O
reference	O
,	O
not	O
the	O
object	O
.	O
</s>
<s>
The	O
clone( )	O
method	B-Language
provides	O
this	O
missing	O
functionality	O
.	O
</s>
<s>
Classes	O
that	O
want	O
copying	O
functionality	O
must	O
implement	O
some	O
method	B-Language
to	O
do	O
so	O
.	O
</s>
<s>
To	O
a	O
certain	O
extent	O
that	O
function	O
is	O
provided	O
by	O
"	O
Object.clone( )	O
"	O
.	O
</s>
<s>
clone( )	O
acts	O
like	O
a	O
copy	B-Language
constructor	I-Language
.	O
</s>
<s>
Typically	O
it	O
calls	O
the	O
clone( )	O
method	B-Language
of	O
its	O
superclass	O
to	O
obtain	O
the	O
copy	O
,	O
etc	O
.	O
</s>
<s>
until	O
it	O
eventually	O
reaches	O
Object	O
's	O
clone( )	O
method	B-Language
.	O
</s>
<s>
The	O
special	O
clone( )	O
method	B-Language
in	O
the	O
base	O
class	O
Object	O
provides	O
a	O
standard	O
mechanism	O
for	O
duplicating	O
objects	O
.	O
</s>
<s>
The	O
class	O
Object	O
's	O
clone( )	O
method	B-Language
creates	O
and	O
returns	O
a	O
copy	O
of	O
the	O
object	O
,	O
with	O
the	O
same	O
class	O
and	O
with	O
all	O
the	O
fields	O
having	O
the	O
same	O
values	O
.	O
</s>
<s>
However	O
,	O
Object.clone( )	O
throws	O
a	O
CloneNotSupportedException	O
unless	O
the	O
object	O
is	O
an	O
instance	O
of	O
a	O
class	O
that	O
implements	O
the	O
marker	B-Language
interface	I-Language
Cloneable	O
.	O
</s>
<s>
The	O
default	O
implementation	O
of	O
Object.clone( )	O
performs	O
a	O
shallow	O
copy	O
.	O
</s>
<s>
When	O
a	O
class	O
desires	O
a	O
deep	O
copy	O
or	O
some	O
other	O
custom	O
behavior	O
,	O
they	O
must	O
implement	O
that	O
in	O
their	O
own	O
clone( )	O
method	B-Language
after	O
they	O
obtain	O
the	O
copy	O
from	O
the	O
superclass	O
.	O
</s>
<s>
The	O
syntax	O
for	O
calling	O
clone	B-Language
in	O
Java	B-Language
is	O
(	O
assuming	O
obj	O
is	O
a	O
variable	O
of	O
a	O
class	O
type	O
that	O
has	O
a	O
public	O
clone( )	O
method	B-Language
)	O
:	O
</s>
<s>
which	O
provides	O
the	O
typecasting	O
needed	O
to	O
assign	O
the	O
general	O
Object	O
reference	O
returned	O
from	O
clone	B-Language
to	O
a	O
reference	O
to	O
a	O
MyClass	O
object	O
.	O
</s>
<s>
One	O
disadvantage	O
with	O
the	O
design	O
of	O
the	O
clone( )	O
method	B-Language
is	O
that	O
the	O
return	O
type	O
of	O
clone( )	O
is	O
Object	O
,	O
and	O
needs	O
to	O
be	O
explicitly	O
cast	O
back	O
into	O
the	O
appropriate	O
type	O
.	O
</s>
<s>
However	O
,	O
overriding	O
clone( )	O
to	O
return	O
the	O
appropriate	O
type	O
is	O
preferable	O
and	O
eliminates	O
the	O
need	O
for	O
casting	O
in	O
the	O
client	O
(	O
using	O
covariant	O
return	O
types	O
,	O
since	O
J2SE	O
5.0	O
)	O
.	O
</s>
<s>
Another	O
disadvantage	O
is	O
that	O
one	O
often	O
cannot	O
access	O
the	O
clone( )	O
method	B-Language
on	O
an	O
abstract	O
type	O
.	O
</s>
<s>
Most	O
interfaces	O
and	O
abstract	O
classes	O
in	O
Java	B-Language
do	O
not	O
specify	O
a	O
public	O
clone( )	O
method	B-Language
.	O
</s>
<s>
As	O
a	O
result	O
,	O
often	O
the	O
clone( )	O
method	B-Language
can	O
only	O
be	O
used	O
if	O
the	O
actual	O
class	O
of	O
an	O
object	O
is	O
known	O
,	O
which	O
is	O
contrary	O
to	O
the	O
abstraction	O
principle	O
of	O
using	O
the	O
most	O
generic	O
type	O
possible	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
one	O
has	O
a	O
List	O
reference	O
in	O
Java	B-Language
,	O
one	O
cannot	O
invoke	O
clone( )	O
on	O
that	O
reference	O
because	O
List	O
specifies	O
no	O
public	O
clone( )	O
method	B-Language
.	O
</s>
<s>
Actual	O
implementations	O
of	O
List	O
like	O
ArrayList	O
and	O
LinkedList	O
all	O
generally	O
have	O
clone( )	O
methods	O
themselves	O
,	O
but	O
it	O
is	O
inconvenient	O
and	O
bad	O
abstraction	O
to	O
carry	O
around	O
the	O
actual	O
class	O
type	O
of	O
an	O
object	O
.	O
</s>
<s>
There	O
are	O
alternatives	O
to	O
clone( )	O
,	O
notably	O
the	O
use	O
of	O
a	O
copy	B-Language
constructor	I-Language
-	O
a	O
constructor	O
that	O
accepts	O
as	O
a	O
parameter	O
another	O
instance	O
of	O
the	O
same	O
class	O
-	O
or	O
a	O
factory	B-Language
method	I-Language
.	O
</s>
<s>
(	O
However	O
,	O
clone( )	O
is	O
often	O
not	O
adequate	O
either	O
for	O
the	O
same	O
reason	O
,	O
as	O
most	O
abstract	O
classes	O
do	O
not	O
implement	O
a	O
public	O
clone( )	O
method	B-Language
.	O
)	O
</s>
<s>
Also	O
the	O
use	O
of	O
serialization	O
and	O
deserialization	O
is	O
an	O
alternative	O
to	O
using	O
clone	B-Language
.	O
</s>
<s>
As	O
a	O
result	O
,	O
the	O
class	O
must	O
not	O
be	O
allowed	O
to	O
make	O
a	O
clone	B-Language
.	O
</s>
<s>
To	O
prevent	O
this	O
,	O
one	O
can	O
override	O
the	O
clone( )	O
method	B-Language
using	O
the	O
following	O
code	O
:	O
</s>
<s>
This	O
is	O
only	O
necessary	O
if	O
a	O
superclass	O
implements	O
a	O
public	O
clone( )	O
method	B-Language
,	O
or	O
to	O
prevent	O
a	O
subclass	O
from	O
using	O
this	O
class	O
's	O
clone( )	O
method	B-Language
to	O
obtain	O
a	O
copy	O
.	O
</s>
<s>
Classes	O
do	O
n't	O
usually	O
inherit	O
a	O
public	O
clone( )	O
method	B-Language
because	O
Object	O
does	O
n't	O
have	O
a	O
public	O
clone( )	O
method	B-Language
,	O
so	O
it	O
is	O
usually	O
unnecessary	O
to	O
explicitly	O
implement	O
a	O
non-functional	O
clone( )	O
method	B-Language
.	O
</s>
<s>
To	O
provide	O
a	O
properly	O
cloneable	O
object	O
of	O
any	O
type	O
,	O
the	O
clone( )	O
method	B-Language
must	O
be	O
both	O
declared	O
correctly	O
and	O
implemented	O
correctly	O
according	O
to	O
the	O
convention	O
described	O
in	O
Object.clone( )	O
.	O
</s>
<s>
1	O
)	O
Every	O
type	O
that	O
needs	O
to	O
be	O
cloned	O
must	O
have	O
a	O
public	O
clone( )	O
method	B-Language
in	O
its	O
own	O
class	O
or	O
a	O
publicly	O
accessible	O
clone( )	O
method	B-Language
in	O
one	O
of	O
its	O
parent	O
classes	O
.	O
</s>
<s>
To	O
invoke	O
clone( )	O
on	O
varY1	O
,	O
which	O
is	O
of	O
type	O
Y	O
,	O
then	O
Y	O
or	O
a	O
parent	O
of	O
Y	O
must	O
declare	O
a	O
publicly	O
accessible	O
clone( )	O
method	B-Language
.	O
</s>
<s>
Here	O
,	O
it	O
is	O
the	O
parent	O
class	O
X	O
that	O
provides	O
the	O
public	O
clone( )	O
method	B-Language
.	O
</s>
<s>
2	O
)	O
Every	O
class	O
that	O
implements	O
clone( )	O
should	O
call	O
super.clone( )	O
to	O
obtain	O
the	O
cloned	O
object	O
reference	O
.	O
</s>
<s>
If	O
the	O
class	O
has	O
any	O
object	O
references	O
that	O
must	O
be	O
cloned	O
as	O
well	O
(	O
when	O
deep	O
copying	O
,	O
for	O
example	O
)	O
,	O
then	O
the	O
clone( )	O
method	B-Language
should	O
perform	O
any	O
required	O
modifications	O
on	O
the	O
object	O
before	O
returning	O
it	O
.	O
</s>
<s>
(	O
Since	O
Object.clone( )	O
returns	O
an	O
exact	O
copy	O
of	O
the	O
original	O
object	O
,	O
any	O
mutable	B-Application
fields	O
such	O
as	O
collections	O
and	O
arrays	O
would	O
be	O
shared	O
between	O
the	O
original	O
and	O
the	O
copy	O
-	O
which	O
in	O
most	O
cases	O
would	O
neither	O
be	O
expected	O
nor	O
desired	O
.	O
)	O
</s>
<s>
Since	O
class	O
Z	O
contains	O
an	O
object	O
reference	O
,	O
its	O
clone( )	O
method	B-Language
also	O
clones	B-Language
that	O
object	O
reference	O
in	O
order	O
to	O
return	O
a	O
deep	O
copy	O
of	O
the	O
original	O
.	O
</s>
<s>
If	O
every	O
class	O
in	O
a	O
hierarchy	O
implements	O
a	O
clone( )	O
method	B-Language
,	O
all	O
of	O
these	O
functions	O
will	O
be	O
called	O
upon	O
cloning	O
,	O
adding	O
some	O
overhead	O
.	O
</s>
<s>
If	O
the	O
purpose	O
of	O
a	O
specific	O
clone( )	O
implementation	O
is	O
not	O
fully	O
understood	O
by	O
consumers	O
,	O
it	O
may	O
unintentionally	O
break	O
the	O
"	O
single	O
object	O
,	O
multiple	O
references	O
"	O
paradigm	O
.	O
</s>
<s>
Generally	O
,	O
clone( )	O
is	O
incompatible	O
with	O
final	O
fields	O
.	O
</s>
<s>
Because	O
clone( )	O
is	O
essentially	O
a	O
default	O
constructor	O
(	O
one	O
that	O
has	O
no	O
arguments	O
)	O
it	O
is	O
impossible	O
to	O
assign	O
a	O
final	O
field	O
within	O
a	O
clone( )	O
method	B-Language
;	O
a	O
compiler	O
error	O
is	O
the	O
result	O
.	O
</s>
<s>
Where	O
the	O
value	O
of	O
the	O
field	O
is	O
an	O
immutable	B-Application
object	I-Application
this	O
is	O
okay	O
;	O
just	O
let	O
the	O
'	O
constructor	O
 '	O
copy	O
the	O
reference	O
and	O
both	O
the	O
original	O
and	O
its	O
clone	B-Language
will	O
share	O
the	O
same	O
object	O
.	O
</s>
<s>
But	O
where	O
the	O
value	O
is	O
a	O
mutable	B-Application
object	I-Application
it	O
must	O
be	O
deep	O
copied	O
.	O
</s>
<s>
Another	O
alternative	O
method	B-Language
is	O
actually	O
making	O
the	O
idea	O
formal	O
:	O
creating	O
a	O
copy	B-Language
constructor	I-Language
that	O
takes	O
an	O
instance	O
.	O
</s>
<s>
In	O
fact	O
that	O
is	O
what	O
is	O
recommended	O
over	O
clone	B-Language
by	O
some	O
people	O
.	O
</s>
