1

I know I should implement the Cloneable interface and then override the clone() method of the Object class in Test, and this is not my problem . I just do not understand why compiler gives "clone() has protected access in object" error while the Test class is extending the Object!

public class Test extends Object{
public static void main(String[] args) throws CloneNotSupportedException  {
     Object o = new Object();
     o.clone(); }  }
John
  • 201
  • 1
  • 7
  • See also https://stackoverflow.com/questions/1138769/why-is-the-clone-method-protected-in-java-lang-object – lexicore Dec 30 '17 at 10:09

1 Answers1

0

The clone method is protected in java.lang.Object. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

ps. You will be able to call super.clone() from an overridden clone method of the Test class. Also make your Test class extends Cloneable.

lexicore
  • 42,748
  • 17
  • 132
  • 221