I am not fully understanding the idea of returning super.clone() in the clone() method of a class. First of all, wouldn't that relate to it returning an object that is a superclass which contains LESS data than requested, because a superclass "is not a" subclass, but a subclass "is a" superclass. And if there were a long chain of subclasses, each calling super.clone(), why wouldn't that lead to it eventually calling Object.clone() at the root of the chain, which isn't any of the subclasses?
Sorry if that was confusing; I confuse myself sometimes
The implementation of clone() in Object checks if the actual class implements Cloneable, and creates an instance of that actual class.
So if you want to make your class cloneable, you have to implement Cloneable and downcast the result of super.clone() to your class. Another burden is that the call to super.clone() can throw a CloneNotSupportedException that you have to catch, even though you know it won't happen (since your class implements Cloneable).
The Cloneable interface and the clone method on the Object class are an obvious case of object-oriented design gone wrong.
Consider this: you have a chain of inheriting classes. each may (or may not) have its own variables. what clone does, as opposed to the equals operator (==) that duplicates the reference, is a cloned copy of the object with a new reference.
For the example above, you would like to clone the last object in the chain.
As the last object is constructed of its superclasses, where each may have a different cloning method implementation, it makes a lot of sense to call the superclass implementation of clone to receive first a cloned parent object before cloning own object.
Another terms that are usually associated with cloning is shallow and deep cloning.
shallow cloning refer to the creation of an exact replica of an object, while deep cloning creates a replica of an object and any child object that the original object refers to.
More on cloning at this link
Read the javadoc of Object.clone() more carefully : it returns a copy of the object. The copy is another instance of the same class as that of the object on which clone is invoked. I.e. foo.clone().getClass() == foo.getClass().
Related
What is the specific reason that clone() is defined as protected in java.lang.Object?
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.
It is protected because the default implementation does a shallow memberwise copy of all fields (including private), circumventing constructor. This is not something an object might be designed to handle in the first place (for example, it might keep track of created object instances in a shared list, or something similar).
For the same reason, the default implementation of clone() will throw if the object it's called on doesn't implement Cloneable. It's a potentially unsafe operation with far-reaching consequences, and therefore author of the class must explicitly opt-in.
From the javadoc of cloneable.
* By convention, classes that implement this interface (cloneable) should override
* <tt>Object.clone</tt> (which is protected) with a public method.
* See {#link java.lang.Object#clone()} for details on overriding this
* method.
* Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
* Therefore, it is not possible to clone an object merely by virtue of the
* fact that it implements this interface. Even if the clone method is invoked
* reflectively, there is no guarantee that it will succeed.
So you could call clone on every object but this would give you most of the time not the results you want or an exception. But is only encouraged if you implement cloneable.
Clone() method has a check internally 'instance of Cloneable or not'.This is how Java team might thought will restrict the improper use of clone() method.clone() method is protected i.e. accessed by subclasses only. Since object is the parent class of all sub classes, so Clone() method can be used by all classes infact if we don't have above check of 'instance of Cloneable'. This is the reason Java team might have thought to restrict the improper use of clone() by having the check in the clone() method 'is it instance of Cloneable'.
Hence whatever classes implement cloneable can use clone() method of Object class.
Also since it made protected, it is available to only those sub classes that implements cloneable interface. If we want to make it public, this method has to be overridden by the sub class with their own implementation of it.
Yes, same problem that I met.
But I solve it by implementing this code
public class Side implements Cloneable {
public Side clone() {
Side side = null;
try {
side = (Side) super.clone();
} catch (CloneNotSupportedException e) {
System.err.println(e);
}
return side;
}
}
Just as the before someone said.
Well, also the sun developers are only human, and they did indeed make huge mistake to implement the clone method as protected, the same mistake as they implemented a non-functioning clone method in ArrayList! So, in general, there exist a much deeper misunderstanding of even experienced Java programmers about the clone method.
However, I've recently found a fast and easy solution to copy any object with all its content, no matter how it is built and what it contains, see my answer here: Bug in using Object.clone()
Again, Java JDK framework shows brilliant thinking:
Cloneable interface does not contain a "public T clone();" method because it acts more like an attribute (eg. Serializable) which allows an instance it to be cloned.
There is nothing wrong with this design because:
Object.clone() will not do what you want with your custom-defined class.
If you have Myclass implements Cloneable => you overwrite clone() with
"public MyClass clone()"
If you have MyInterface extends Cloneable and some MyClasses implementing MyInterface:
simply define "public MyInterface clone();" in the interface and every method using MyInterface objects will be able to clone them, no matter their MyClass-class.
What is the specific reason that clone() is defined as protected in java.lang.Object?
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.
It is protected because the default implementation does a shallow memberwise copy of all fields (including private), circumventing constructor. This is not something an object might be designed to handle in the first place (for example, it might keep track of created object instances in a shared list, or something similar).
For the same reason, the default implementation of clone() will throw if the object it's called on doesn't implement Cloneable. It's a potentially unsafe operation with far-reaching consequences, and therefore author of the class must explicitly opt-in.
From the javadoc of cloneable.
* By convention, classes that implement this interface (cloneable) should override
* <tt>Object.clone</tt> (which is protected) with a public method.
* See {#link java.lang.Object#clone()} for details on overriding this
* method.
* Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
* Therefore, it is not possible to clone an object merely by virtue of the
* fact that it implements this interface. Even if the clone method is invoked
* reflectively, there is no guarantee that it will succeed.
So you could call clone on every object but this would give you most of the time not the results you want or an exception. But is only encouraged if you implement cloneable.
Clone() method has a check internally 'instance of Cloneable or not'.This is how Java team might thought will restrict the improper use of clone() method.clone() method is protected i.e. accessed by subclasses only. Since object is the parent class of all sub classes, so Clone() method can be used by all classes infact if we don't have above check of 'instance of Cloneable'. This is the reason Java team might have thought to restrict the improper use of clone() by having the check in the clone() method 'is it instance of Cloneable'.
Hence whatever classes implement cloneable can use clone() method of Object class.
Also since it made protected, it is available to only those sub classes that implements cloneable interface. If we want to make it public, this method has to be overridden by the sub class with their own implementation of it.
Yes, same problem that I met.
But I solve it by implementing this code
public class Side implements Cloneable {
public Side clone() {
Side side = null;
try {
side = (Side) super.clone();
} catch (CloneNotSupportedException e) {
System.err.println(e);
}
return side;
}
}
Just as the before someone said.
Well, also the sun developers are only human, and they did indeed make huge mistake to implement the clone method as protected, the same mistake as they implemented a non-functioning clone method in ArrayList! So, in general, there exist a much deeper misunderstanding of even experienced Java programmers about the clone method.
However, I've recently found a fast and easy solution to copy any object with all its content, no matter how it is built and what it contains, see my answer here: Bug in using Object.clone()
Again, Java JDK framework shows brilliant thinking:
Cloneable interface does not contain a "public T clone();" method because it acts more like an attribute (eg. Serializable) which allows an instance it to be cloned.
There is nothing wrong with this design because:
Object.clone() will not do what you want with your custom-defined class.
If you have Myclass implements Cloneable => you overwrite clone() with
"public MyClass clone()"
If you have MyInterface extends Cloneable and some MyClasses implementing MyInterface:
simply define "public MyInterface clone();" in the interface and every method using MyInterface objects will be able to clone them, no matter their MyClass-class.
I'm reading J. Bloch's effective Java and now I'm at the clone method. He mentioned that no constructors were called. But I didn't find it in the javadoc. The second thing is that:
The provision that
“no constructors are called” is too strong. A well-behaved clone
method can call constructors to create objects internal to the clone
under construction. If the class is final, clone can even return an
object created by a constructor.
It seems a little contradictory to me. While he was mentioning that no constructors are called, he said that well-behaved clone can call constructors.
Couldn't you explain what he meant?
There are two different things:
Your own implementation of clone() method in your own class which should follow some rules.
An implementation of existing Object.clone() method.
I believe, when J. Bloch speaks about "no constructors were called", he assumes the behavior of existing Object.clone() method. But the quotation you cite is about your own implementation of clone() method which in some cases can construct new objects using constructor or even return such object.
Classes implementing Cloneable interface can call clone() method from Object class without having CloneNotSupportedException. The signature of the clone() in Object class tells why constructors are not called if you call the inherited clone. It is native code, not pure Java.
protected native Object clone() throws CloneNotSupportedException;
And note that, this is just a shallow copy implementation.
IHello !
I want to clone an object "B" who has a supertype "A".
In the clone() method of "B", I call the super.clone() (method of "A"). This method returns a "A" type with the fields cloned and setted.
In the clone() method of "B", I get the "pre-construct" returned object from super.clone() and I want to continue to clone it. So I must cast it into a "B" type object.
No problem at compilation time but it crashes a ClassCastException at runtime.
Is it possible to turn an object into its subtype ?
Or must I clone all the fields (even the "A" class fields) in the "B" clone() method ?
Thanks.
Proper cloning requires the use of a virtual method, which is implemented in the lowest level that supports cloning, and--depending upon how cloning is implemented--is either overridden by every level beyond that which adds new fields that would require deep cloning, or is overridden by every derived class, period.
If all implementations of clone worked by calling super.clone, up to the point where the lowest-level class calls object.clone, then calling clone on a derived type which has been cast to the base type would yield an object of that derived type. Unfortunately, the belief that super.clone is broken has resulted in many people using copy constructors instead of calling super.clone, thus creating an unfortunate self-fulfilling prophesy.
Given that unfortunate situation, the best one can do is probably to have each class whose parent exposes a clone method that might not chain to object.clone, expose a protected copy constructor which takes an instance of its type, chains to the parent class's copy constructor, copies any fields added by that derived type which are publicly exposed (or map to properties that are) or refer to immutable objects, and clones any fields of that derived type which are not publicly exposed and refer to mutable objects. This approach will work, but it will be necessary for every class derived from a cloneable class to implement its own override of the virtual clone method, even if the only thing that method does is call the protected copy constructor of its own class.
If your parent class has a non-virtual cloning method which calls some kind of constructor without virtual dispatch, then it is not possible to properly implement any derived class.
Thanks for your answers, but I've found a solution :
I didn't know that in my clone() method of my super class, I had to get my object by calling super.clone() (native clone() from Object Class ?) and cast it into my type. ( I did explicit instanciation MySuperType t = new MySuperType() ).
Now, I get a MySuperType that I can downcasting in my sub class :
(MySubClass) sc = (MySubClass) super.clone();
Thank you, so I will write a post in my blog www.patate-chaude.fr !
Consider the following code from The Java Programming Language book
public class MyClass extends HerClass implements Cloneable {
public MyClass clone()
throws CloneNotSupportedException {
return (MyClass) super.clone();
}
// ...
}
When the overiding clone() function already species the return type as MyClass then what is the requirement of specifying it again in the return statement ?
Also since the clone of Myclass's super class object is being created (cause clone() is being called wrt superclass), how can it be of Myclass type?
Thanks in advance
Because clone() returns an object of class Object, and you must cast it to the correct type. But you know it is an object of type MyClass, so that cast is correct.
In theory you're right: as you have to specify the type of function return values the compiler could try and perform the correction automatically. On the other hand requiring an explicit conversion helps identify possible errors.
Unless you have specific requirements the clone() method of the Object class already does the right thing, i.e. it creates an object of the correct class and copies all the non-static attributes in the cloned object. However it cannot return it as a derived type because at compile time that type is not known to the Object class itself.
It is true that the clone() method could have been provided automatically for all classes, but sometimes you don't want it to be available and at other times you want to override the default behaviour; for instance you might have an id attribute in your class that you want to be different for each instance of your class even when cloned. Having to override the clone() method gives you a place where you can implement such functionality.
This is because the clone() method in Object returns an Object. However you can return your subclass in clone() because it extends an Object. If the method in MyClass looked like this
public Object clone()
Then it would still be a valid cloneable object and it would work. You wouldn't need to cast anything. The interface, Cloneable is just a marker interface, which means it doesn't actually have any methods.
Your easy question first: why is super.clone() cast to MyClass? That's because the declaration of HerClass.clone() specified a returned value of HerClass, so you must cast it to the right type.
Now, for the more difficult question: how can super.clone() actually return an instance of MyClass? I actually had a hard time finding the answer, but I did somewhat find an answer in Effective Java by Joshua Bloch. There is still some "magic" in the background of Object.clone() that I don't quite understand.
Item 11 from the book:
In practice, programmers assume that if they extend a class and invoke
super.clone from the subclass, the returned object will be an instance
of the subclass. The only way a superclass can provide this
functionality is to return an object obtained by calling super.clone.
If a clone method returns an object created by a constructor, it will
have the wrong class. Therefore, if you override the clone method in a
nonfinal class, you should return an object obtained by invoking
super.clone. If all of a class’s superclasses obey this rule, then
invoking super.clone will eventually invoke Object’s clone method,
creating an instance of the right class.
I originally tried to answer your question by writing a program without knowing you always had to call super.clone(). My homemade clone method for HerClass was returning a new instance of HerClass generated from a constructor (new HerClass()). The code compiled, but it failed at execution when I was trying to cast (MyClass) super.clone(). Only methods that are chained down from Object.clone() can return a value that is an instance of one of their subtype.
Note that if HerClass.clone() is not explicitly implemented, by default it simply returns Object.clone(). The default method has protected access, but since you are calling it from a subclass, it's not a problem.