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


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

Re: Why it necessary to implement clonealbe interface?

Newsgroups comp.lang.java.programmer
Date 2015-11-25 11:01 -0800
References <n31vi5$dkg$1@speranza.aioe.org> <n321jk$3uj$1@dont-email.me> <n330jp$5ba$1@speranza.aioe.org>
Message-ID <84654386-4302-4358-9143-5280e2a7111d@googlegroups.com> (permalink)
Subject Re: Why it necessary to implement clonealbe interface?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Tuesday, November 24, 2015 at 7:50:28 PM UTC-5, Arne Vajhøj wrote:
> On 11/24/2015 11:03 AM, Eric Sosman wrote:
> > 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.
> 
> To this excellent explanation I would just like to add that
> today (today as in last decade) instead of a marker interfaces
> one would usually use an annotation. But Cloneable goes back
> to Java 1.0.
> 
> Arne

So you think it does just look stupid because it was written into Java 1.0?
My only other thought for wanting an interface class was to say whether or not you want your cloneable object to return the condition
if (myObject instanceof Cloneable) {

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