Here is something that I cannot understand.
In java.lang.Object
the clone()
is defined with protected
modifier. By definition than it can be accessed by name inside its own class definition, by name inside any class derived from it, and by name in the definition of any class in the same package.
Here the Sample
class is in another package, and obviously it can't access clone()
from the Object
class. But as Sample
derives implicitly from Object
, why is it not able to access it? The definition doesn't say that it HAS to satisfy both conditions (inside same package AND also to be a subclass).
public class Sample {
public Object foo() throws CloneNotSupportedException {
...
return someObject.clone();
}
}
In your case, the clone()
method is not visible because you are not calling it from a subclass. Sample
derives from Object
, so it can access its own clone()
method, but not that of other objects.
Object clone()
was designed with several mistakes. So it is not a good practice to use it - it is very hard to get it right:
clone()
making it public, it will still fail, because each class has to implement Cloneable
Cloneable
, however, does not define any methods, so the users of the objects can't refer to it as Cloneable
and expect a clone method.super.clone()
for the default cloning mechanism to workCheck this question for alternatives.
Works for me: http://ideone.com/eST8Y
import java.util.*;
import java.lang.*;
class Main
{
public Object foo() throws CloneNotSupportedException
{
return this.clone();
}
public static void main (String[] args) throws java.lang.Exception
{
new Main().foo();
}
}
This compiles without error. It still throws a runtime CloneNotSupportedException
because Main
does not implement Cloneable
.
A class implements the
Cloneable
interface to indicate to theObject.clone()
method that it is legal for that method to make a field-for-field copy of instances of that class.Invoking Object's clone method on an instance that does not implement the
Cloneable
interface results in the exceptionCloneNotSupportedException
being thrown.
@Bozho's answer is really the right answer here, though. Just don't use Object.clone()
.
See Effective Java, Item 10: Override clone
judiciously (Item 11 in later editions).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With