I am trying to clone a object of class Integer, which does implement the cloneable inteface.
Integer a = new Integer(4);
Integer b = a.clone();
I know there are work arounds for this, but I must implement it like this.
why I am getting this error =
clone()
has protected access in java.lang.Object
Why would it say this? Isn't the clone method a public abstract method of clonable interface, what does it have to do with object. Thanks in advance :-)
Sure, all methods in Object
are inherited. The clone
method however is protected
, thus only accessible from within subclasses.
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.
The reason why you can call clone()
in most other circumstances is that the subclass "opens it up" by overriding it with the public
access modifier.