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


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

Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?
Date 2015-05-29 08:24 -0400
Organization A noiseless patient Spider
Message-ID <mk9lnt$qjm$1@dont-email.me> (permalink)
References <646d1d0e-4c63-439f-840c-3c4421e7bc15@googlegroups.com> <2ecb0338-074f-431d-8aea-c20e5f16ff93@googlegroups.com>

Show all headers | View raw


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

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


Thread

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

csiph-web