Cloneable Interface , StringBuilder and StringBuffer cannot be cloned
Let’s come to direct point to point using java program
public class Test implements Cloneable{ // line 5 int a; @Override public String toString() { // line 7 return"CloneClass [a=" + a + "]"; } @Override protected Object clone() throws CloneNotSupportedException {// line 6 // TODO Auto-generated method stub return super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { //4 Test c1=new Test (); // line 1 c1.a=10; // Line 2 Test c2=(Test)c1.clone(); //line 3 System.out.println(c1.a); System.out.println(c2.a); } // Line 8 }
Line 1.create the object of CloneClass.
Line 2. Assign the value through reference at
runtime.
Line 3. Using through clone method we passed C1 reference inside C2 reference, now it’s
showing compile time error, let’s see line no-4.
Line 4. Please check this practically now we have
need to define try/catch block or throws , because its showing compile time
error. Simply resolve this error using throwsCloneNotSupportedException .
Till line No-4 Its work fine no Compile
time error but when we run this program then its showing runtime error.
Line 5.It’s
must be implement CloneableInterface .
What if we
don’t implement Cloneable interface?
The program
would throw CloneNotSupportedException if we don’t implement the Cloneable
interface.
Line 6 . Need
for Overriding clone() method of Object class. it’swoking fine even without
override Clone() method. but, its not good practice.
If not Overriding clone() method then what happen.
If you don't, and say just return new CloneClass(this.x);, then that works fine for instances of CloneClass. But if someone extends CloneClassit's no longer possible for them to get an instance of the right class when overriding your clone method. The one thing Object.clone does that you can't do a good job of yourself is creating an instance of the right class; the rest is just copying instance fields, which is drudgework you could have done yourself if you wanted.
Line 7. Now,
we override toString() method for printing the object reference.
Line 8- Free all the memory and execution of the
program. because main() method terminates and the stack memory created for
main() method is destroyed.4
In above
Program define Step by Step clone() method and Cloneable() Interface
Now, let’s
start guy’s more aboutclone() method.
It is a bad
idea to make an immutable object Cloneable. String is not Cloneable. Immutable
Integer and Decimal are also not Cloneable.
But here little confusion about StringBuilder and StringBuffer cannot be cloned, Even both are mutable.
The clone
method in the Object class has protected access which means it can only be
called from within the class itself, another class in the same package or a
subclass.
String and
StringBuilder,StringBuffer don't override clone, therefore the same applies
when you try to call the clone method on variables of those types.
Your class is not in the same package as String or StringBuilder and does not extend them, so you can't call the clone method from within your class on variables of those types.
No comments: