John
John

Reputation: 201

Overriding Clone() method in Java

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(); }  }

Upvotes: 1

Views: 830

Answers (1)

lexicore
lexicore

Reputation: 43709

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.

Upvotes: 0

Related Questions