Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the clone() method protected in java.lang.Object?

Tags:

java

oop

clone

People also ask

What is the purpose of following code protected object clone ()?

Why use clone() method ? The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing time to be performed that is why we use object cloning.

What does clone () do in Java?

The class Object 's clone() method creates and returns a copy of the object, with the same class and with all the fields having the same values. However, Object. clone() throws a CloneNotSupportedException unless the object is an instance of a class that implements the marker interface Cloneable .

What is the purpose of the following code 1 protected object clone () throws Clonenot supported exception?

Class CloneNotSupportedException Thrown to indicate that the clone method in class Object has been called to clone an object, but that the object's class does not implement the Cloneable interface.

Is it necessary to override clone method in Java?

Therefore, while overriding the clone() method it is recommended to declare it public instead of protected so that it is access-able from a class in any location in the system. While overriding methods the method in the child class must not have higher access restrictions than the one in the superclass.


The fact that clone is protected is extremely dubious - as is the fact that the clone method is not declared in the Cloneable interface.

It makes the method pretty useless for taking copies of data because you cannot say:

if(a instanceof Cloneable) {
    copy = ((Cloneable) a).clone();
}

I think that the design of Cloneable is now largely regarded as a mistake (citation below). I would normally want to be able to make implementations of an interface Cloneable but not necessarily make the interface Cloneable (similar to the use of Serializable). This cannot be done without reflection:

ISomething i = ...
if (i instanceof Cloneable) {
   //DAMN! I Need to know about ISomethingImpl! Unless...
   copy = (ISomething) i.getClass().getMethod("clone").invoke(i);
}

Citation From Josh Bloch's Effective Java:
"The Cloneable interface was intended as a mixin interface for objects to advertise that they permit cloning. Unfortunately it fails to serve this purpose ... This is a highly atypical use of interfaces and not one to be emulated ... In order for implementing the interface to have any effect on a class, it and all of its superclasses must obey a fairly complex, unenforceable and largely undocumented protocol"


The Clonable interface is just a marker saying the class can support clone. The method is protected because you shouldn't call it on object, you can (and should) override it as public.

From Sun:

In class Object, the clone() method is declared protected. If all you do is implement Cloneable, only subclasses and members of the same package will be able to invoke clone() on the object. To enable any class in any package to access the clone() method, you'll have to override it and declare it public, as is done below. (When you override a method, you can make it less private, but not more private. Here, the protected clone() method in Object is being overridden as a public method.)


clone is protected because it is something that ought to be overridden so that it is specific to the current class. While it would be possible to create a public clone method that would clone any object at all this would not be as good as a method written specifically for the class that needs it.


The Clone method can't be directly used on any object, which is why it is intended to be overriden by the subclass.

Of course it could be public and just throw an appropriate exception when cloning is not possible, but i think that would be misleading.

The way clone is implemented right now makes you think about why you want to use clone, and how you want your object to be cloned.


IMHO it's as simple as this:

  • #clone must not be called on non-cloneable objects, therefore it is not made public
  • #clone has to be called by subclasses ob Object that implement Cloneable to get the shallow copy of the right class

What's the right scope for methods that shall be callable by subclasses, but not by other classes?

It's protected.

Classes implementing Cloneable of course will make this method public so it can be called from other classes.