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


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

Re: Are you sure, Mr. Gosling?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Are you sure, Mr. Gosling?
Date 2016-06-08 14:10 -0400
Organization A noiseless patient Spider
Message-ID <nj9n32$5gt$1@dont-email.me> (permalink)
References (11 earlier) <746532a2-c6db-4d58-afc0-1cc57440eff5@googlegroups.com> <nj7asv$j9j$1@dont-email.me> <9ecc34ba-fac1-45ed-88cb-71403c7c906d@googlegroups.com> <nj9arf$l3v$1@dont-email.me> <a682cfb6-e880-481d-a9de-1be42b5d7d50@googlegroups.com>

Show all headers | View raw


On 6/8/2016 1:21 PM, Eric Douglas wrote:
>[...]
>>      You may choose to specify that your clone() implementation returns
>> a Gizmo instead of an Object (if that's your preference), and assuming
>> the returned object is obtained from super.clone() then yes: At some
>> point along the line somebody must apply a cast.  However, I'd suggest
>> returning an Object instead, because that puts the fewest constraints
>> on the clone() methods of subclasses -- they might want to return a
>> non-Gizmo, and if your class' clone() returns Gizmo they'll be unable
>> to override it.  If your class is final, yeah, maybe then you could
>> have clone() return a Gizmo -- but if not, I'd strongly recommend
>> sticking with Object.  (It would be rather strange for a clone() to
>> return something of a different class than the cloned "leaf" object,
>> but clone() is itself rather strange, IMHO.  Stay on the safe side.)
>
> example?  If my Gizmo clone returns a Gizmo it would be fine for a non-Gizmo to return a non-Gizmo as long as the non-Gizmo is an extension of a Gizmo.  Why would an extended class return anything from a clone which is not an instance of itself or it's super?  I would think the only time it makes sense to return the default Object is if something could really need to get a cloned instance as an Object not knowing or caring what it's class may be.  If you're only ever going to use a cloned Gizmo as a Gizmo it seems easier to cast it before you pass it out than to cast it on every call to the clone method if there may be multiple places to call it from.

     (Shrug.)  As I said, it would be "rather strange."  And as I've said
elsethread I personally am not a big fan of Cloneable and have never
implemented it in any of my own classes, so although I've made myself
familiar with the rules (had to fix somebody else's clone() once), my
view on how "best" to follow them should be taken with a grain of
salt.  YMMV, as they say.

-- 
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

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