-3

User Rohit Jain commented

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.

How do they know that, is there a Java specification or something else?

Community
  • 1
  • 1
  • From here, SEE http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java – PigeonIsBigBird May 28 '15 at 23:00
  • 4
    **Of course there's a Java specification.** What on earth is the question here? – user207421 May 28 '15 at 23:03
  • @EJP, What I mean, is Why do we need to “@Override Object clone” if all classes are subclasses of Object, why can we not directly invoke the function? I saw somebody has said that SENTENCE, but I do not why, so I posted it here. I know there is Java Specification, I mean, if like that people said, where is the section in the Java Specification, I do not find it. Or There is some other reasons, why Why do we need to “@Override Object clone” if all classes are subclasses of Object, why can we not directly invoke the function? – PigeonIsBigBird May 28 '15 at 23:08
  • You can directly invoke it. – JamesB May 28 '15 at 23:13
  • @JamesB If I use for clone of Object, it can not. A a_Clone = (A) clone() can not be complied, Netbeans said: clone() has protected access in Objects....... – PigeonIsBigBird May 28 '15 at 23:21
  • @JamesB the whole code is like : ...A implements Cloneable........ A a = new A(); A a_Clone = (A) a.clone(); COMPILION ERROR: clone() has protected access in Objects. WHY! – PigeonIsBigBird May 28 '15 at 23:23
  • You only need to override it to make it public, if you want to call it from outside the class being cloned, or if you want to add your own code to what `super.clone()` does. There is also Javadoc. You don't seem to have done any research into this whatsoever. Your question was most unclear. Don't blame others for that, or for saying so. And don't SHOUT here. People are trying to sleep. – user207421 May 29 '15 at 01:21
  • @EJP I see a lot Javadoc. I make my quetion more clearly, all objects in java are subclasses of Objects, if so, why do we need override the clone() method and make it public and then we can call it, why can we direct involke that method, because that method is my super class? – PigeonIsBigBird May 29 '15 at 06:53

1 Answers1

1

I'm sorry people are rude and downvote without explanation. SO used to be better than this...people were nicer to new SO members.

To answer your question, yes, there is a specification. That Object is a superclass of every other class is mentioned in the API and in the Java Language Specification (JLS).

As a new Java programmer you should look in the API when you have questions about classes...that will tell you what you need to know, at least for your first few years programming in Java.

Oracle has a tutorial that will teach you the basics of Java, and there are numerous other resources on the web.

The purpose of @Override is not to enable you to call the superclass' method. It provides a measure of compile-time safety by ensuring that the method annotated with @Override actually overrides or implements a method in a supertype or a public method in Object, as noted in the API. You can override a method without using the @Override annotation, however, if the overridden method's signature changes in the future you probably won't notice until your code doesn't work as intended. It's much better to catch these sorts of issues at compile time. Also, @Override helps other programmers understand your code.

Good luck!

Paul
  • 19,704
  • 14
  • 78
  • 96
  • thank you Paul....MY QUESTION IS NOT JAVA SPECIFICATION; BUT THIS [COMMENT](http://stackoverflow.com/questions/13092128/why-to-override-clone-method-in-java#comment17791043_13092175) IS WHY! – PigeonIsBigBird May 28 '15 at 23:32