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


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

Re: Are you sure, Mr. Gosling?

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Are you sure, Mr. Gosling?
Date 2016-06-07 23:25 +0200
Message-ID <droseuF3h3gU1@mid.individual.net> (permalink)
References (8 earlier) <drm6bcFhke1U1@mid.individual.net> <5c864ac4-9634-4c88-9d6b-d14dbf3b3886@googlegroups.com> <drodg0FejbU1@mid.individual.net> <746532a2-c6db-4d58-afc0-1cc57440eff5@googlegroups.com> <nj7dbg$shs$1@dont-email.me>

Show all headers | View raw


On 07.06.2016 23:12, Eric Sosman wrote:
> On 6/7/2016 1:22 PM, Eric Douglas wrote:
>> On Tuesday, June 7, 2016 at 1:10:36 PM UTC-4, Robert Klemme wrote:
>>> That sounds more like a use case for clone() to me as it seems to stress
>>> that the original type is preserved.  It really depends on the usage.
>>
>> Maybe I'm getting confused then.  I thought clone() sounded wrong
>> because it should return an instance of that class, so if you extend
>> the class and call super.clone it gets an instance of the base class?
>> If your extended class needs to return an instance of itself, which
>> needs to copy all the same values as the super class plus a few more...

The usual implementation invokes super.clone() exactly as Eric has shown 
in this posting.  And Object.clone() makes sure the type of the instance 
is the same as the receiver of the method.

> The clone() calls will percolate all the way up to Object's clone(),
> which will check that the originating object implements Cloneable and
> them return a field-for-field copy of that originating object, with the
> originating object's class.  (In this instance the intervening clone()
> calls just pass that object along, so the class of the eventual object
> is BaseClass or ExtendedClass, depending on where things started.)
>
>      But if one or more of obj{1,2,3} is unshareable, the clone() methods
> must provide suitable substitutes: clones or copies of the unshareable
> objects.  Let's suppose the odd-numbered fields must be unique; then
> you'd have something like
>
>      public class BaseClass implements Cloneable {
>          // ...
>          @Override
>          public Object clone() throws CloneNotsupportedException {
>              BaseClass cloned = (BaseClass) super.clone();
>              cloned.obj1 = independentCopyOf(obj1);
>              // obj2 can be shared; just leave it alone.
>              return cloned;
>          }
>          // ...
>      }
>
>      public class ExtendedClass extends BaseClass {
>          // ...
>          @Override
>          public Object clone() throws CloneNotsupportedException {
>              ExtendedClass cloned = (ExtendedClass) super.clone();
>              cloned.obj3 = independentCopyOf(obj3);
>              return cloned;
>          }
>          // ...
>      }

One note though: I would catch CloneNotsupportedException in the 
BaseClass implementation of clone() because the exception cannot be 
thrown.  It just pollutes the signature of clone() unnecessarily.

>      Finally, the clone() methods I've shown simply pass along the
> object created by Object's clone(), whose class will be the same as
> that of the ultimate "leaf" class of the clone operation.  That's a
> fairly normal state of affairs, but it is *not* enforced, nor even
> required.  It is entirely possible (though perverse) that the clone()
> method applied to a Woozle object might instead return a Grizzle,
> or even a String.  Or even null.  You have to know what to expect
> from the chain of superclasses.

Any reasonable implementation would _not_ deviate from the convention to 
propagate clone() calls in the way you have shown.  Doing that opens the 
box of Pandora _and_ the Gates of Hell (and I am not talking about Bill).

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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


Thread

Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 15:27 -0400
  Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-04 16:43 -0400
    Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 18:44 -0400
      Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-05 08:05 -0400
        Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-05 08:31 -0400
        Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-06 05:22 -0700
          Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-06 09:00 -0400
            Re: Are you sure, Mr. Gosling? v_borchert@despammed.com (Volker Borchert) - 2016-06-06 16:06 +0000
              Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-06 22:56 +0200
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 05:08 -0700
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 08:41 -0400
                Re: Are you sure, Mr. Gosling? Martin Gregorie <martin@address-in-sig.invalid> - 2016-06-07 13:14 +0000
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 06:30 -0700
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 19:10 +0200
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 10:22 -0700
                Re: Are you sure, Mr. Gosling? Mike Amling <mamling@chaff.us> - 2016-06-07 15:31 -0500
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 06:31 -0700
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 10:41 -0400
                Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 10:21 -0700
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 14:10 -0400
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 20:54 +0200
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 16:36 -0400
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 23:11 +0200
                Re: Are you sure, Mr. Gosling? Lew <lewbloch@gmail.com> - 2016-06-10 12:20 -0700
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-14 19:10 +0200
                Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 17:12 -0400
                Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 23:25 +0200
                Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-07 17:33 -0400
          Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-06 21:18 -0400

csiph-web