Cloneable Interface Java

If you would have watched some Sci-Fi movies or typical Bollywood movies, you know what Cloning means. Yeah! making an identical copy of something. Since Java tends to simulate the real-world scenarios pretty well, Cloning is one of them also exists in Java.

Your JVM when you use ‘new’ keyword too many times to clone objects

You guessed it right, we clone objects using Cloneable interface and we do it without calling ‘new’ again and again, which is a pretty expensive operation by the way for complex objects especially as a good programmer you cannot afford to do that. Cloneable is an interface in Java which needs to be implemented by a class to allow its objects to be cloned. To put in the language of Oracle Documentation :

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.

If you will try to Clone an object which doesn’t implement the Cloneable interface, it will CloneNotSupportedException, which you will want to handle.

#1

Let’s jump into an example to get a better picture,

Lets make a StormTrooper class :

public class StormTrooper {

String color = "White";
String master = "Darth Vader";

StormTrooper() {
System.out.println("Welcome to the Dark Side");
}
}

In your main program :

public static void main(String[] args) {

StormTrooper stormTrooper1 = new StormTrooper();
StormTrooper stormTrooper2 = stormTrooper1;

System.out.println("StormTrooper 1 : " + stormTrooper1.master);
System.out.println("StormTrooper 2 : " + stormTrooper2.master);

stormTrooper2.master = "Skywalker";

System.out.println("StormTrooper 1 : " + stormTrooper1.master);
System.out.println("StormTrooper 2 : " + stormTrooper2.master);


}

This gives the following output :

Welcome to the Dark SideStormTrooper 1 : Darth Vader
StormTrooper 2 : Darth Vader
StormTrooper 1 : Skywalker
StormTrooper 2 : Skywalker

Here you create a StormTrooper soldier1 using the ‘new’ keyword, but then you create another object called soldier2 and refer it to soldier1. Note that all the changes you make to soldier2 are being reflected back to soldier1 and vice versa.

#2

Let's try cloning object through the Cloneable Interface, so we implement the Cloneable interface to our class.

Remember we also will have to handle the exception, so for this tutorial I am just gonna add the exception handling to the main method signature. But in practice, you should handle it in Try Catch (From main method at-least).

This code gives the following output :

Welcome to the Dark Side// Object3 is already clones hereStormTrooper 1 : Darth Vader : White
StormTrooper 2 : Darth Vader : White
StormTrooper 3 : Darth Vader : White
// We change object 1, 2 is affected. object3 remains the same as it is already clones and is living in a separate reference.StormTrooper 1 : Skywalker : black
StormTrooper 2 : Skywalker : black
StormTrooper 3 : Darth Vader : White
// Cloning object4 out of changed object1StormTrooper 4 : Skywalker : black

So you got a tiny glimpse of how we are cloning objects without calling new operator and we are making them quite independent of each other.

#3

That is not all that Cloneable has to offer, right now our objects are being completely copied. But what if you want to restrict various fields will that have to remain the same let's say the ‘Master Name’ in our example. Colour can be changed while being cloned, the master will not be changed. It can be modified manually later but while being cloned it will remain the same for all the cloned objects.

So for that, we need to override the implemented Clone method in our Stormtrooper Class.

If you understood, what just happened here, on line 15 clone a new object from calling (super) object. Then we are explicitly telling the new ‘master’ to copy the master name of the super object to the new cloned object. Hence, this is how you use Clone.

DeeJay Out!

Developer who loves to build beautiful apps and write great Medium stories to share what I have learned.