7

In Java, to make a class cloneable, we need to implement Cloneable interface. Implementing this interface, is just to say that this class supports cloning.

But what is the motive of Java language designers for not making "allowed-to-clone" as default functionality of each class?

We have default implementation for shallow copy already present. Then why this restriction ?

Vikdor
  • 23,934
  • 10
  • 61
  • 84
Kaushik Lele
  • 6,439
  • 13
  • 50
  • 76

4 Answers4

5

Think about cloning an object with nested properties. How deep recursively do you want to go? This could be tough for the memory, so the developers left it for us to decide.

eugen-fried
  • 2,111
  • 3
  • 27
  • 48
  • 1
    Default implementation created shallow copy, so there is no question of "how deep", developers want to go. Question is if language is providing default mechanism for shallow copy, then why not allow using it as it is? Why an extra step of Clonable – Kaushik Lele Dec 02 '12 at 15:56
  • Think of clone() on an object with reference on object2. Now you call copy.getObject2().setX(x). You've changed object2 for both of them (because it's still a reference in default shallow copy. So that means that clone() is a dangerous method, so it's better to mark it, as another line of errors defence. – eugen-fried Dec 02 '12 at 16:14
4

It's a marker interface to let Java know that implementing class is intentionally being designed for cloning(similar use as of other marker interfaces). If you read further, then you find below:

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.

You need to provide custom method for cloning. By having the interface Cloneable, Java is aware that you are intentionally supporting cloning of your object. By providing your custom clone method, you are over-ridding the default clone method of the object.

This way, you get the flexibility to decide(Mark), which objects can be cloned and which not. If clone-able then up to what level(very useful in object graph cases).

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Question is if language is providing default mechanism for shallow copy, then why not allow using it as it is? Why an extra step of implementing Clonable? – Kaushik Lele Dec 02 '12 at 15:57
  • @KaushikLele That is what I tried to explain. Shallow copy is light weight process where you are not creating all new objects in memory (you are reusing the the memory references). Here you are creating new objects. In that case, that interface provides a mechanism where you are able to decide what to copy and what not. This becomes very useful when your dealing with long object graphs(nested objects). – Yogendra Singh Dec 02 '12 at 16:03
0

Many reasons stand in the way, the chief one being that cloning is not a problem solvable for the general case, much like serialization.

The shallow copy you get by default will in many cases destroy the object's invariants so it is out of the picture as a general, default cloning mechanism.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Question is if language is providing default mechanism for shallow copy, then why not allow using it as it is? Why an extra step of implementing Clonable? – Kaushik Lele Dec 02 '12 at 15:58
  • As I said, because this would force this method on the contract of each and every Java object, and te default shallow copy **breaks the class invariants** of the majority of Java classes. This means that the class would be forced to implement against its will. Some objects aren't even theoretically clonealble. Think about the *Singleton* pattern for a moment. – Marko Topolnik Dec 02 '12 at 16:19
0

The "Cloneable" interface is part of a design pattern called a "Marker Class". Basically, within the clone method will be some reference to a type "Cloneable". When you implement the cloneable interface, it means that your class can be referenced as type "Cloneable".

The other reason, in practicality, is that you override the "clone()" method, and clone in your own specific way. That means the data that you deem important is present in the new class.

christopher
  • 26,815
  • 5
  • 55
  • 89