how many types of cloning?

Answer Posted / vineesh ktda

1. Difference between shallow copy and deep copy
. 2. How to implement shallow copy ?
3. How to mplement deep copy? .
A shallow copy creates an identical copy (new instance) of an object .If an object contains reference (pointer) type fields pointing to other objects then it copies only the references and not the objects to which they point. The result is two objects that point to the same contained object. That means the original object and cloned object maintains a seperate copy for value (primtive) type fields , references and shares common place for value of reference type fields. So in shallow copy , the changes in the value of reference type fields of cloned object also reflects in the original objects and vice versa. Shallow copy can be implemented by using the clone method of an Object class
A deep copy creates an identical copy of an object including the references and the objects they point to , as well as any references or objects contained within that and so on. In deep copy , the changes in the value of reference type fields of cloned object does not reflect in the original objects and vice versa.. There is no direct method for deep copy . But there are many ways to implement the deep copy.
1. You can use constructor of an object to create a second object with property values taken from the first object.
2. You can use clone method of an Object class to create a shallow copy of an object , and make it deep copy by assigning new objects whose values are the same as the original object to reference type fields. The DeepCopy method in the example illustrates this approach.
3. Use Serialization technique ie. Serialize the object , and then deserialize to a different object variable which implements deep copy.

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many types of java are there?

537


What is compareto () in java?

529


What is preflight request?

516


How do you find the independent variable?

498


What is the default value of the local variables?

491






How to make a write-only class in java?

562


What is string example?

586


What is the scope or life time of instance variables?

644


What is byte code and why is it important to java’s use for internet programming?

582


Which method you will use to create a new file to store some log data. Each time a new log entry is necessary, write string to the file in java ?

616


What is java util function?

501


What is mean by exception?

537


What is the symbol for average?

503


Explain the difference between collection api and stream api in java8?

491


What class allows you to read objects directly from a stream?

849