Skip to main content

Posts

Showing posts with the label Cloning

Java Cloning Interview Questions

Q1) What are different types of cloning in Java? Ans) Java supports two type of cloning: - Deep and shallow cloning. By default shallow copy is used in Java. Object class has a method clone() which does shallow cloning. Q2) What is Shallow copy? Ans) In shallow copy the object is copied without its contained objects. Shallow clone only copies the top level structure of the object not the lower levels. It is an exact bit copy of all the attributes.       The shallow copy is done for obj and new object obj1 is created but contained objects of obj are not copied. It can be seen that no new objects are created for obj1 and it is referring to the same old contained objects. If either of the containedObj contain any other object no new reference is created