What is the point in letting my class implement Cloneable?

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.


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().

Tags:

Java

Cloneable