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


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

Re: Are you sure, Mr. Gosling?

Newsgroups comp.lang.java.programmer
Date 2016-06-08 10:21 -0700
References (10 earlier) <drodg0FejbU1@mid.individual.net> <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>
Message-ID <a682cfb6-e880-481d-a9de-1be42b5d7d50@googlegroups.com> (permalink)
Subject Re: Are you sure, Mr. Gosling?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Wednesday, June 8, 2016 at 10:42:04 AM UTC-4, Eric Sosman wrote:
> > A copy constructor has the same amount of runtime/memory overhead as a clone method?
> 
>      "Probably not, except when it does.  That is to say, Yes.  Or No."
> 
>      In other words, either a constructor or a clone() method can execute
> as many or as few statements as it likes, churn through as much or as
> little memory as it likes, do as much I/O as it likes, query as many
> web servers in Elbonia as it likes, ...

That was to say the same overhead by default, if you put in the clone method which only returns the value of super.clone(), versus if you create a copy constructor which does nothing more than copy the values exactly the same as the super.clone is doing.  Of course the overhead is going to differ once you start adding custom logic.  As I say, the copy constructor only copies exactly what you tell it; the clone method will copy everything, then after you call super.clone() you can tell it to overwrite the default values.

>      As for the "default" behavior of clone() -- Well, the "default"
> behavior is to create and throw a CloneNotSupportedException.  If you
> implement Cloneable and write a clone() method, that method has some
> kind of behavior -- but it's hard to call it a "default" behavior.

Of course you have to override clone() or it will throw an exception by default.  The default behavior is what it does when you tell it to do only that, as in the previous sample:
@Override
public Object clone() { 
     try { 
          return super.clone(); 
     } catch (final CloneNotSupportedException c) { 
          // Not likely. 
          throw new IllegalStateException(); 
     } 
} 

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

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