Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33194

Re: Why it necessary to implement clonealbe interface?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Why it necessary to implement clonealbe interface?
Date 2015-11-24 11:03 -0500
Organization A noiseless patient Spider
Message-ID <n321jk$3uj$1@dont-email.me> (permalink)
References <n31vi5$dkg$1@speranza.aioe.org>

Show all headers | View raw


On 11/24/2015 10:28 AM, Xue Feng wrote:
> A class has to implement cloneable interface, otherwise its object can
> not be cloned. IMO, it is enough that a class clone itself by inheriting
> the clone method from Object class. It is somewaht reduntant to
> implement cloneable interface. Actually, there is nothing in cloneable
> interface. Namely, it is defined as follows:
>
> interface cloneable {
> }
>
> I wonder the reason for this design. Any explanation? Thanks.

     Object's clone() is a protected method, so it can be called only
from a subclass of Object.  Since every class descends from Object
this may not seem like much of a restriction, but it is: If classes
X and Y both inherit clone() from object, then X's clone() and Y's
clone() are also protected.  X can call its own clone() and can call
Object's clone(), but it cannot call Y's clone -- because Y's clone()
is protected, and X is not a subclass of Y.  Similarly, Y can call
its own clone() and Object's clone(), but cannot call X's clone().
So, mere inheritance of the clone() method is not enough to make it
usable outside one branch of the inheritance tree.

     Also, Object's clone() throws CloneNotSupportedException if the
object being cloned is of a non-Cloneable class.  The Cloneable
interface is a "marker," a notification that the class has provided
for its own cloning.  (The "marker interface" pattern is unusual, and
some would even call it an anti-pattern, but cloning is an unusual
operation to begin with.)  By implementing Cloneable, a class announces
to Java: "I have decided what it means to make a `clone' of my objects,
and have made arrangements to create such clones."  Usually, the
arrangements involve overriding the inherited clone() so as to do
some class-specific things; the class might also choose to make its
overriding clone() method public.

     There's a good explanation of Cloneable -- advantages, drawbacks,
and gotchas -- in "Effective Java" by Bloch, a book I feel every
serious Java programmer should own or at least study.  I strongly
suggest you obtain a copy, and believe you will find it highly valuable.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why it necessary to implement clonealbe interface? Xue Feng <xf.lotus@yahoo.com> - 2015-11-24 23:28 +0800
  Re: Why it necessary to implement clonealbe interface? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-24 11:03 -0500
    Re: Why it necessary to implement clonealbe interface? Arne Vajhøj <arne@vajhoej.dk> - 2015-11-24 19:49 -0500
      Re: Why it necessary to implement clonealbe interface? Eric Douglas <e.d.programmer@gmail.com> - 2015-11-25 11:01 -0800
        Re: Why it necessary to implement clonealbe interface? Arne Vajhøj <arne@vajhoej.dk> - 2015-11-25 16:41 -0500
  Re: Why it necessary to implement clonealbe interface? Lew <lewbloch@gmail.com> - 2015-11-29 23:00 -0800

csiph-web