Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34547
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Are you sure, Mr. Gosling? |
| Date | 2016-06-07 17:12 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <nj7dbg$shs$1@dont-email.me> (permalink) |
| References | (7 earlier) <nj471k$f77$2@Gaia.teknon.de> <drm6bcFhke1U1@mid.individual.net> <5c864ac4-9634-4c88-9d6b-d14dbf3b3886@googlegroups.com> <drodg0FejbU1@mid.individual.net> <746532a2-c6db-4d58-afc0-1cc57440eff5@googlegroups.com> |
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...
>
> example:
> public class BaseClass{
> private Obj1 obj1;
> private Obj2 obj2;
> public BaseClass(final BaseClass copyMe) {
> setObj1(copyMe.getObj1());
> setObj2(copyMe.getObj2());
Aside: Don't call overridable methods from a constructor.
> }
> public Obj1 getObj1(){
> return obj1;
> }
> public Obj2 getObj2(){
> return obj2;
> }
> public void setObj1(final Obj1 newObj){
> obj1=newObj;
> }
> public void setObj2(final Obj2 newObj){
> obj2=newObj;
> }
> }
> public class ExtendedClass extends BaseClass{
> private Obj3 obj3;
> public ExtendedClass(final ExtendedClass copyMe){
> super(copyMe);
> setObj3(copyMe.getObj3());
> }
> public Obj3 getObj3(){
> return obj3;
> }
> public void setObj3(final Obj3 newObj){
> obj3=newObj;
> }
> }
>
> How would you do the clone()?
It depends on whether the obj{1,2,3} instances should be shared
by the original object and its clone, or whether they need to be
independent. (Bloch offers an example of a stack implementation with
an internal array; if you clone the stack object without also cloning
the array, both stacks will share the same array ...)
In the first case (obj{1,2,3} are shareable), all you need do
is make both classes Cloneable:
public class BaseClass implements Cloneable {
// ...
@Override
public // or protected, if you prefer
Object clone() throws CloneNotsupportedException {
return super.clone();
}
// ...
}
public class ExtendedClass extends BaseClass {
// ...
@Override
public Object clone() throws CloneNotsupportedException {
return super.clone();
}
}
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;
}
// ...
}
You'll see I'm waving my hands a bit with that independentCopyOf()
fiction. It might be `cloned.obj1 = obj1.clone()' -- in which case the
fields should probably be Cloneable's rather than Object's -- or it
might be an invocation of a copy constructor -- in which case, again,
the fields should be more specific about their classes so you can do
`cloned.obj1 = new Class1(obj1)' and so on. Or it might be something
else altogether, depending on what you mean by a "copy" of your object.
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.
--
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
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