Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why classes in Java are not cloneable by default

Tags:

java

cloning

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 ?

like image 411
Kaushik Lele Avatar asked Dec 02 '12 15:12

Kaushik Lele


1 Answers

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.

like image 138
eugen-fried Avatar answered Oct 05 '22 01:10

eugen-fried