Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31747 > unrolled thread
| Started by | pigeon.tian@googlemail.com |
|---|---|
| First post | 2015-05-28 16:07 -0700 |
| Last post | 2015-05-28 23:49 -0700 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? pigeon.tian@googlemail.com - 2015-05-28 16:07 -0700
Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? Arne Vajhøj <arne@vajhoej.dk> - 2015-05-28 20:50 -0400
Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? HU Tiange <pigeon.tian@googlemail.com> - 2015-05-28 23:48 -0700
Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-05-29 08:24 -0400
Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? Arne Vajhøj <arne@vajhoej.dk> - 2015-06-03 20:41 -0400
Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? Lew <lewbloch@gmail.com> - 2015-06-19 16:45 -0700
Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? HU Tiange <pigeon.tian@googlemail.com> - 2015-05-28 23:49 -0700
| From | pigeon.tian@googlemail.com |
|---|---|
| Date | 2015-05-28 16:07 -0700 |
| Subject | Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin? |
| Message-ID | <646d1d0e-4c63-439f-840c-3c4421e7bc15@googlegroups.com> |
I find a question from stackoverflow, what is I want to ask,
http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java
Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?
One Comment say that :
" For invoking any method of super class you need an instance of that class. Now, if you override the clone method, you have that instance of Object class as super. So, you use super.clone() to invoke it."
That is to say, we must do such things?
But:
class Animal
{
void A(){};
}
class Dog extends Animal
{
}
main method:
Dog dog = new Dog();
dog.A;
It works, I do not invoke some super class methods.
Why in clone situation, we need?
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-05-28 20:50 -0400 |
| Message-ID | <5567b7e2$0$284$14726298@news.sunsite.dk> |
| In reply to | #31747 |
On 5/28/2015 7:07 PM, pigeon.tian@googlemail.com wrote:
> I find a question from stackoverflow, what is I want to ask,
>
> http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java
>
>
> Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?
>
> One Comment say that :
>
> " For invoking any method of super class you need an instance of that class. Now, if you override the clone method, you have that instance of Object class as super. So, you use super.clone() to invoke it."
>
> That is to say, we must do such things?
> But:
> class Animal
> {
> void A(){};
> }
>
> class Dog extends Animal
> {
>
> }
>
> main method:
> Dog dog = new Dog();
> dog.A;
>
> It works, I do not invoke some super class methods.
> Why in clone situation, we need?
Object clone is protected so usually it would not be accessible for
that caller.
And there is a good reason for that as Object clone rarely would be
a good clone implementation
The Java API docs actually explains a little bit:
<quote>
By convention, the returned object should be obtained by calling
super.clone. If a class and all of its superclasses (except Object) obey
this convention, it will be the case that x.clone().getClass() ==
x.getClass().
By convention, the object returned by this method should be independent
of this object (which is being cloned). To achieve this independence, it
may be necessary to modify one or more fields of the object returned by
super.clone before returning it. Typically, this means copying any
mutable objects that comprise the internal "deep structure" of the
object being cloned and replacing the references to these objects with
references to the copies. If a class contains only primitive fields or
references to immutable objects, then it is usually the case that no
fields in the object returned by super.clone need to be modified.
</quote>
Arne
[toc] | [prev] | [next] | [standalone]
| From | HU Tiange <pigeon.tian@googlemail.com> |
|---|---|
| Date | 2015-05-28 23:48 -0700 |
| Message-ID | <2ecb0338-074f-431d-8aea-c20e5f16ff93@googlegroups.com> |
| In reply to | #31747 |
On Friday, May 29, 2015 at 1:07:30 AM UTC+2, HU Tiange wrote:
> I find a question from stackoverflow, what is I want to ask,
>
> http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java
>
>
> Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?
>
> One Comment say that :
>
> " For invoking any method of super class you need an instance of that class. Now, if you override the clone method, you have that instance of Object class as super. So, you use super.clone() to invoke it."
>
> That is to say, we must do such things?
> But:
> class Animal
> {
> void A(){};
> }
>
> class Dog extends Animal
> {
>
> }
>
> main method:
> Dog dog = new Dog();
> dog.A;
>
> It works, I do not invoke some super class methods.
> Why in clone situation, we need?
But all objects are subclasses of Objects, why it can not be directly called?
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-05-29 08:24 -0400 |
| Message-ID | <mk9lnt$qjm$1@dont-email.me> |
| In reply to | #31749 |
On 5/29/2015 2:48 AM, HU Tiange wrote:
> On Friday, May 29, 2015 at 1:07:30 AM UTC+2, HU Tiange wrote:
>> I find a question from stackoverflow, what is I want to ask,
>>
>> http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java
>>
>>
>> Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?
>>
>> One Comment say that :
>>
>> " For invoking any method of super class you need an instance of that class. Now, if you override the clone method, you have that instance of Object class as super. So, you use super.clone() to invoke it."
>>
>> That is to say, we must do such things?
>> But:
>> class Animal
>> {
>> void A(){};
>> }
>>
>> class Dog extends Animal
>> {
>>
>> }
>>
>> main method:
>> Dog dog = new Dog();
>> dog.A;
>>
>> It works, I do not invoke some super class methods.
>> Why in clone situation, we need?
>
> But all objects are subclasses of Objects, why it can not be directly called?
Dog is a subclass of Animal, so Dog can call Animal's protected
clone() method.
Cat is a subclass of Animal, so Cat can call Animal's protected
clone() method.
... but Cat is not a subclass of Dog, so Cat cannot call Dog's
protected clone() method. Also, Dog is not a subclass of Cat, so
Dog cannot call Cat's protected clone() method.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-06-03 20:41 -0400 |
| Message-ID | <556f9ec8$0$302$14726298@news.sunsite.dk> |
| In reply to | #31751 |
[sorry if duplicate post]
On 5/29/2015 8:24 AM, Eric Sosman wrote:
> On 5/29/2015 2:48 AM, HU Tiange wrote:
>> On Friday, May 29, 2015 at 1:07:30 AM UTC+2, HU Tiange wrote:
>>> I find a question from stackoverflow, what is I want to ask,
>>>
>>>
http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java
>>>
>>> Why do we need to "@Override Object clone" if all classes are
>>> subclasses of Object, why can not directly involke the functin?
>>>
>>> One Comment say that :
>>>
>>> " For invoking any method of super class you need an instance of that
>>> class. Now, if you override the clone method, you have that instance
>>> of Object class as super. So, you use super.clone() to invoke it."
>>>
>>> That is to say, we must do such things?
>>> But:
>>> class Animal
>>> {
>>> void A(){};
>>> }
>>>
>>> class Dog extends Animal
>>> {
>>>
>>> }
>>>
>>> main method:
>>> Dog dog = new Dog();
>>> dog.A;
>>>
>>> It works, I do not invoke some super class methods.
>>> Why in clone situation, we need?
>>
>> But all objects are subclasses of Objects, why it can not be directly
>> called?
>
> Dog is a subclass of Animal, so Dog can call Animal's protected
> clone() method.
>
> Cat is a subclass of Animal, so Cat can call Animal's protected
> clone() method.
>
> ... but Cat is not a subclass of Dog, so Cat cannot call Dog's
> protected clone() method. Also, Dog is not a subclass of Cat, so
> Dog cannot call Cat's protected clone() method.
I think I am starting to understand the question and here is
an attempt to answer.
package oofun;
public class P {
protected void M1() {
System.out.println("P.M1");
}
protected void M2() {
System.out.println("P.M2");
}
}
package oofun.ns1;
import oofun.P;
public class C1 extends P {
protected void M2() {
System.out.println("C1.M2");
}
public void test() {
M1(); // standard inheritance: call of P.M1
M2(); // standard override: call of C1.M2
C1 tmp = new C1();
tmp.M1(); // standard inheritance: call of P.M1 - a different
instance does not matter
tmp.M2(); // standard override: call of C1.M2 - a different
instance does not matter
}
}
package oofun.ns2;
import oofun.P;
import oofun.ns1.C1;
public class C2 extends P {
public void test() {
M1(); // standard inheritance: call of P.M1
M2(); // standard override: call of P.M2
C2 tmp = new C2();
tmp.M1(); // standard inheritance: call of P.M1 - a different
instance does not matter
tmp.M2(); // standard override: call of P.M2 - a different
instance does not matter
// now the fun starts
C1 x = new C1();
// why can't C2.test call P.M1 when C1 is a sub class of P?
//x.M1();
// this is the reason: here P.M2 is overriden by C1.M2 ad C2 is
not a sub class of C1
//x.M2();
}
}
Arne
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2015-06-19 16:45 -0700 |
| Message-ID | <22b5bf0d-33cc-4ac9-b683-9940cb83baf8@googlegroups.com> |
| In reply to | #31758 |
> > On 5/29/2015 2:48 AM, HU Tiange wrote: > >> On Friday, May 29, 2015 at 1:07:30 AM UTC+2, HU Tiange wrote: > >>> I find a question from stackoverflow, what is I want to ask, > >>> > >>> > http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java > >>> > >>> Why do we need to "@Override Object clone" if all classes are > >>> subclasses of Object, why can not directly involke the functin? In addition to what the others said: The 'clone()' method is treated specially, like a few other things in Java such as 'java.io.Serializable' and 'java.lang.String'. As Arne pointed out, two subclasses of a parent type can call the parent's 'protected' methods, but not each other's. But 'clone()' is treated specially. If a type doesn't implement 'Cloneable', the method will throw an exception. If it doesn't also override the method, the clone probably won't be what is expected. By convention, the override will elevate the method's visibility to 'public', which makes it callable by other types. RTFM: <https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--> <https://docs.oracle.com/javase/8/docs/api/java/lang/Cloneable.html> _Effective Java_, 2nd edition, Item 11, by Joshua Bloch Under no circumstances should you code a 'clone()' method without reading the reference _Effective Java_ chapter and other tips in that book about the method. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | HU Tiange <pigeon.tian@googlemail.com> |
|---|---|
| Date | 2015-05-28 23:49 -0700 |
| Message-ID | <0637b013-277f-420c-b3c5-c495a631800e@googlegroups.com> |
| In reply to | #31747 |
On Friday, May 29, 2015 at 1:07:30 AM UTC+2, HU Tiange wrote:
> I find a question from stackoverflow, what is I want to ask,
>
> http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java
>
>
> Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?
>
> One Comment say that :
>
> " For invoking any method of super class you need an instance of that class. Now, if you override the clone method, you have that instance of Object class as super. So, you use super.clone() to invoke it."
>
> That is to say, we must do such things?
> But:
> class Animal
> {
> void A(){};
> }
>
> class Dog extends Animal
> {
>
> }
>
> main method:
> Dog dog = new Dog();
> dog.A;
>
> It works, I do not invoke some super class methods.
> Why in clone situation, we need?
@arne Vajhoj
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web