ການນໍາສະເຫນີ
The Cloneable interface in java is a marker interface. It does have any method. But if we write a class as below, it gives a message to the jvm that the class can be cloned. The following code shows a simple object cloning process –
[ລະຫັດ]
package com.home.cloning;
ສາທາລະນະ ທີ່ຫ້ອງຮຽນ CloneClass implements Cloneable {
int ເປັນ;
double ຂ;
// This method calls Object’s clone().
CloneClass getClone() {
ພະຍາຍາມ {
// call clone in Object.
return (CloneClass) Super.clone();
} ຈັບ (CloneNotSupportedException e) {
ລະບົບ.ອອກ.println (” Cloning not allowed. ” );
return this;
}
}
}
[/ລະຫັດ]
Let us check the second example below.
[ລະຫັດ]
package com.home.cloning;
ສາທາລະນະ ທີ່ຫ້ອງຮຽນ TestCloneObject {
ສາທາລະນະ void testIface() {
CloneClass x1 = ໃຫມ່ CloneClass();
CloneClass x2;
x1.a = 15;
x1.b = 35.05;
x2 = x1.getClone(); // clone x1
ລະບົບ.ອອກ.println(” x1: ” + x1.a + ” ” + x1.b);
ລະບົບ.ອອກ.println(” x2: ” + x2.a + ” ” + x2.b);
}
ສາທາລະນະ static void ຕົ້ນຕໍ(String args[]) {
TestCloneObject testCloneObject = ໃຫມ່ TestCloneObject();
// test via protected
testCloneObject.testIface();
}
}
In the above example, the method [ລະຫັດ] getClone [/ລະຫັດ] calls the [ລະຫັດ] clone [/ລະຫັດ] method in the object and returns the object. It must be noticed here that the object with is returned after the cloning mechanism has to be type casted into its appropriate type, in this case it is
[ລະຫັດ] CloneClass [/ລະຫັດ]
If the class is not implementing the cloneable interface, and we try to clone that object we get a
[ລະຫັດ] CloneNotSupportedException [/ລະຫັດ]
In the process of cloning, the constructor is not called rather an exact copy of the said object is created. But the object of which the clone is created, must implement the cloneable interface.
The ທີ່ຫ້ອງຮຽນ Object’s clone() method creates and returns a copy of the object, with the same class and with all the fields having the same values. However, [ລະຫັດ] Object.clone() [/ລະຫັດ] throws a [ລະຫັດ] CloneNotSupportedException [/ລະຫັດ] unless the object is an instance of a class that implements the marker interface Cloneable.
The default implementation of [ລະຫັດ] Object.clone()[/ລະຫັດ] performs a shallow copy. If a class requires a deep copy or some other custom behavior, it must have its customized [ລະຫັດ] clone()[/ລະຫັດ] method after they obtain the copy from the superclass.
Advantages of cloning
Cloning mechanism saves extra task of the developer in case we need to create a copy of an object. We do not need to call the [ລະຫັດ] ໃຫມ່ [/ລະຫັດ] operator of the object. Thus cloning saves a lot of extra processing task of developer. A clone of an object is an exact copy of the object.
Disadvantages of cloning
One disadvantage of cloning is that the return type of the [ລະຫັດ] clone [/ລະຫັດ] method is an [ລະຫັດ] Object [/ລະຫັດ]. Hence a type casting is required on the created object.
Another disadvantage is that it is not possible to access the [ລະຫັດ] clone [/ລະຫັດ] method on an abstract type. Most interfaces and abstract classes in Java do not have the specify a public [ລະຫັດ] clone [/ລະຫັດ] ວິທີການ. ເປັນຜົນມາຈາກ, the [ລະຫັດ] clone [/ລະຫັດ] method is used only if the actual class of an object is known, which is against the abstraction principle of using the most generic type possible. For example, if one has a List reference in Java, one cannot invoke [ລະຫັດ] clone [/ລະຫັດ] method on that reference because List specifies no public clone() ວິທີການ. Actual implementations of List like ArrayList and LinkedList all generally have clone() methods themselves, but it is inconvenient and bad abstraction to carry around the actual class type of an object.
Cloning is a potentially dangerous action, as it can have some unintended side effects. e.g, if the object being cloned contains a reference variable say refObject, then in the cloned object, refObject will have the reference of the same object which the original object is referring to. If the clone makes a change in the contents of the refObject, then the change will be reflected in the original object as well. Consider the following example – If an object opens an I/O stream and is then cloned, then both of the two objects will be capable of operating on the same stream. Further, if one of these objects closes the stream, then the stream is closed for both and if the second object tries to write to it, this causes an error.
Since cloning can cause some problems, the [ລະຫັດ] clone [/ລະຫັດ] ວິທີການ. So the [ລະຫັດ] clone [/ລະຫັດ] method should be called from within a class which is implementing the [ລະຫັດ] cloneable [/ລະຫັດ] interface where the method calling the [ລະຫັດ] clone [/ລະຫັດ] method is made protected, or it must be explicitly overridden by the class which is public. In the example above we have seen cloning by making the [ລະຫັດ] clone [/ລະຫັດ] protected. The following example illustrates the cloning methodology via overriding –
[ລະຫັດ]
package com.home.cloning;
ສາທາລະນະ ທີ່ຫ້ອງຮຽນ CloneViaOverRiding implements Cloneable {
int ເປັນ;
double ຂ;
// clone() is now overridden and is public.
ສາທາລະນະ Object clone() {
ພະຍາຍາມ {
// call clone in Object.
return Super.clone();
} ຈັບ (CloneNotSupportedException e) {
ລະບົບ.ອອກ.println(“Cloning not allowed.”);
return this;
}
}
}
[/ລະຫັດ]
Now check the next example below.
[ລະຫັດ]
package com.home.cloning;
ສາທາລະນະ ທີ່ຫ້ອງຮຽນ TestCloneObject {
ສາທາລະນະ void testPublic() {
CloneViaOverRiding x1 = ໃຫມ່ CloneViaOverRiding();
CloneViaOverRiding x2;
x1.a = 10;
x1.b = 20.98;
// here, clone() is called directly.
x2 = (CloneViaOverRiding) x1. clone ();
ລະບົບ.ອອກ.println(“x1: ” + x1.a + ” ” + x1.b);
ລະບົບ.ອອກ.println(“x2: ” + x2.a + ” ” + x2.b);
}
ສາທາລະນະ static void ຕົ້ນຕໍ(String args[]) {
TestCloneObject testCloneObject = ໃຫມ່ TestCloneObject();
// test via ສາທາລະນະ
testCloneObject.testPublic();
}
}
[/ລະຫັດ]
In this example the method, the method [ລະຫັດ] clone [/ລະຫັດ] of the object class is overridden that is why it is declared public in contrast to the earlier example, where the [ລະຫັດ] getClone [/ລະຫັດ] does not have any access modifiers making it accessible only at the package level.
In either of these two approaches, implementing the cloneable interface is mandatory.
The side effects caused by cloning are sometimes difficult to identify in the initial level. It is easy to think that a class is safe for cloning when it actually is not. In general, is not advised to implement the Cloneable interface for any class without having a solid business ground .
Alternative to cloning
Cloning mechanism has few alternatives –
- copy constructor – a copy constructor is a constructor which accepts another instance of the same class as a parameter.
- factory method – these methods are not always adequate when the concrete type of the cloned object is not known in advance.
- Use of serialization and deserialization is another alternative to using clone.
ສະຫຼຸບ
- Object cloning is the mechanism of creating a copy of an existing object
- Cloning follows the shallow copy mechanism
- Closing saves some extra tasks of the developer
- Once the cloning is done the created clone object is required to explicitly casted in to the required type
- Cloning has some side effects e.g if an object which is cloned has a reference to another object, and the new cloned object modifies the object which being referenced, then the original object also gets changed.
- Cloning mechanism has some alternatives – the copy constructor, factory method, serialization and de-serialization.