Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point in letting my class implement Cloneable?

Tags:

java

cloneable

I came across some class code that implements Clonable, the documentation states:

A class implements the Cloneable interface to indicate to the Object.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 exception CloneNotSupportedException being thrown. By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method. Note that this interface does not contain the clone 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.

I can't understand the point in implementing this class, as said in the docs the .clone method is not implemented in the interface, and I have to implement it. So why use this class? Why won't I just write a method copyClass in my class to make the object copy without the implementation of this class?

like image 682
Emil Adz Avatar asked May 12 '13 13:05

Emil Adz


People also ask

Why do we need to implement cloneable interface?

There is a method clone() in the Object class. Cloneable interface is implemented by a class to make Object. clone() method valid thereby making field-for-field copy. This interface allows the implementing class to have its objects to be cloned instead of using a new operator.

Why should we implement cloneable interface if we have to clone an Object though clone method comes from Object class?

Object's clone() method probably just checks it using: this instanceof Cloneable. The reason that the clone() method is defined in the Object class, is because some 'magic' is needed to actually make a clone. First of all, a new object has to be created without the use of a constructor.

What does cloneable mean in java?

Interface Cloneableclone() 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 exception CloneNotSupportedException being thrown.

How many methods are required for a class that implements cloneable?

Basically, if a class wants to be clonable, it just needs to implement the Cloneable interface -- which has one method, the clone() method.


2 Answers

To implement the clone method, you simply do:

public Object clone() throws CloneNotSupportedException {     return super.clone(); } 

You can of course customize the method to make a deeper copy if needed.

Calling super.clone() is almost mandatory because, unless the class is final and thus can't be overridden, the clone() method must return an instance of the same class as the object on which it's called. So simply creating a new instance and copy the state will work for this class, but not for all the subclasses. Moreover, you don't always have access to all the state contained in superclasses.

In short, you make the protected clone method of Object public. And the first thing that the Object.clone() method does is (this is not the real code, but this is what the method does):

if (!(this instanceof Cloneable)) {     throw new CloneNotSupportedException(); } 

So, Cloneable is just a marker interface to let the Object.clone() method know that it must not throw an exception when called.

This is one of the most badly-designed parts of Java. Usually, you should prefer using a copy contructor instead of using clone().

like image 71
JB Nizet Avatar answered Sep 28 '22 03:09

JB Nizet


It allows you to write more generic code. If you have multiple classes implementing Cloneable interface, and want to pass their instances as an argument to method, you don't have to create multiple methods differing with one variable type, you can just use Cloneable t. It's the same with other interfaces. And, the same with every other interface, it's kinda multiple inheritance. Implementing those interfaces makes your code more legible too.

like image 26
Mateusz Avatar answered Sep 28 '22 03:09

Mateusz