Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not cast inside overridden .clone?

Tags:

java

clone

I understand how Java cloning works and the arguments against using it. Lets say I am dead set on using it anyway.

For the sake of convenience I would like to write a clone method for class Foo as follows:

@Override
public Foo clone(){
    Foo f = (Foo)super.clone();
    //Replace mutable fields
    return f;
}

As far as I can tell this would be safe. However, I noticed that Cloneable classes in the API do not do this. Is there a reason for this and is it bad style?

like image 680
yesennes Avatar asked Nov 02 '15 14:11

yesennes


People also ask

Why is clone () protected?

The clone() is protected because it is not declared in the Cloneable interface. Because of this reason, the clone method becomes somewhat useless as it might make the copies of your existing data.

Can we override clone method?

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.

How do I handle CloneNotSupportedException?

How to Handle CloneNotSupportedException. To avoid the CloneNotSupportedException , the Cloneable interface should be implemented by the class whose objects need to be cloned. This indicates to the Object. clone() method that it is legal to create a clone of that class and helps avoid the CloneNotSupportedException .

Why is clone method in object class in Java?

Clone() method in Java. Object cloning refers to the creation of an exact copy of an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object. In Java, there is no operator to create a copy of an object.


2 Answers

Back before Java 5, covariant return types weren't allowed at all, so Foo clone() wouldn't even compile. Since a lot of Cloneable classes have been written a long time ago, that's probably the reason for majority of the cases.

I suspect that newer classes don't use it simply because clone() is known to be quite a handful, and when people rarely do use it, they don't even think that they could cast it. They find some "how to implement clone()" question and write the code in the old fashioned way.

like image 85
Kayaman Avatar answered Oct 02 '22 13:10

Kayaman


Object.clone() creates a new object where all fields have the same value as the original. For fields of reference type, this means that the new field is simply a reference to the same object as the original. It does not clone fields. Therefore in many cases, the default implementation would lead to shared data, which may be undesirable. For example, if ArrayList.clone() used the default implementation, you would end up with two ArrayList instances sharing the same backing array.

If all the fields of an object are immutable, it may be a good idea to simply cast the result of super.clone() to the appropriate type.

like image 36
Paul Boddington Avatar answered Oct 02 '22 15:10

Paul Boddington