If you consider official Oracle Java documentation for Object class you can find that the clone()
method is protected, due to polymorphism this method will be able only in the same package or in the child class, but not the outside it.
So, the method would be able only in package java.lang
and only in all children of this class, but not in the packages where children has been declared. Try to read this topic to rise you understanding about java access modifiers
But there is one thick here: you can make override of this method like this
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
and after that you be able to use method clone
in the same package where declared your class MyClass
, but be prepared to get CloneNotSupportedException
Also you be able to use your own implementation. Just implement Clonable
interface and provide your own clone
method.
Also, if you planing that the clonned object must be equals you also should override equals
method too.
Good Luck!