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


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

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

Newsgroups comp.lang.java.programmer
Date 2015-06-19 16:45 -0700
References <646d1d0e-4c63-439f-840c-3c4421e7bc15@googlegroups.com> <2ecb0338-074f-431d-8aea-c20e5f16ff93@googlegroups.com> <mk9lnt$qjm$1@dont-email.me> <556f9ec8$0$302$14726298@news.sunsite.dk>
Message-ID <22b5bf0d-33cc-4ac9-b683-9940cb83baf8@googlegroups.com> (permalink)
Subject Re: Why do we need to "@Override Object clone" if all classes are subclasses of Object, why can not directly involke the functin?
From Lew <lewbloch@gmail.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?

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

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