Vous êtes sur la page 1sur 4

[Date] [Document title]

[Document subtitle]

Khushi
[COMPANY NAME]
Contents
Task 3: ........................................................................................................................................ 2
Java clone mechanism: ........................................................................................................... 2
why you think the Java language requires the cloning mechanism. ...................................... 2
Task 3:
*Java clone mechanism:
Java clone mechanism: In Java clone is used to create a copy of an object by using clone ().
The clone is a complicated method from java. lang. Object class and this is used to create a
copy of an object in java.
*why do we require cloning mechanism in java language?
If we want the exact copy of an object, then clone method is used Clone (). It also saves times
to create same copy of the object and if we do it by using the new keyword then it takes a lot
of time to process so, that’s by we use object Cloning.
Basically, we can do something that java language can’t do means it can copy same state of the
object, plus its real class place. The cloning mechanism in java based on each class calling the
superclass's clone method.
The Syntax for calling a clone method in java is:
My Class copy = (My Class) obj. clone ();

Example of how the cloning mechanism is implemented in Java.

public class MyClone {


int x;
public static void main(String[] args) throws CloneNotSupportedException {
MyClone c = new MyClone();
MyClone a = (MyClone) c.clone(); // Type-cast is required
}
}

*A discussion and illustration by providing an example of how the cloning mechanism is


implemented in Java.

*Different options available to developers for implementing cloning when cloning


inheritance hierarchies.
*Some advantages and disadvantages of the cloning mechanism.
Advantages:

1. If there are two object obj1 and obj2

Any changes in obj2 will reflect on obj1 but any changes in obj1 will not reflect in obj2.
But if we will write obj2 = obj1.clone(); any changes in obj2 will reflect on obj1 and vice
versa.
If we mentioned obj2 =obj1.clone(); then, any changes occur in obj2 then it automatically
reflects in obj1 same for obj2.

2. Its easy to create clone object as come to create new operator.


3. Cloning method is a fastest way to create a copy of arrays.
4. Cloning method requires very less lines code it just needs abstract class only with 3,4
lines long clone() method if we don't need deep copy.
Disadvantages:
1. We must handle deep cloning very wisely because if we do a small mistake in clone
then it will make the clone fail fully.
2. If we add any new field to the class and class requires a change in the clone method

Reference:
https://www.google.com/search?rlz=1C1CHBF_enNZ824NZ824&ei=NNKhXJCiGYuf9QPYurzgDg&q=ja
va+clone+mechanism&oq=java+clone+mechanism&gs_l=psy-
ab.3..0i71l8.0.0..12794...0.0..0.0.0.......0......gws-wiz.O41cUtNmsss

https://en.wikipedia.org/wiki/Clone_(Java_method)

https://www.youth4work.com/Talent/Core-Java/Forum/109211-what-is-use-of-object-cloning

https://dzone.com/articles/java-cloning-with-example

Vous aimerez peut-être aussi