Link to home
Start Free TrialLog in
Avatar of Manikandan Thiagarajan
Manikandan ThiagarajanFlag for India

asked on

cloneable

I am preparing for an interview i need to brush up my skills

cloneable means it would create an copy of Insance.

could you explain types of cloneable and give  me a pseudo code for understanding purpose
Avatar of fritzfrancis
fritzfrancis
Flag of United States of America image

I am a C# programmer so I am not sure if the Clone() method works the same as in Java. In C# if a class implements the ICloneable interface, then it must implement a Clone() method. This method, not being generic, returns an object. The object, depending on the implementer, represents a shallow, or deep, copy of the orginal object. At least, in the case of .NET, Microsoft may get rid of the ICloneable just for the reason that you can't tell whether or not you are getting a shallow or deppy copy of an object.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is some code:

public class Class1 : ICloneable
{
      public string Property1 { get; set; }
   
      public object Clone()
      {
            return new Class1() { Property1 = this.Property1 };
      }
}
Avatar of Manikandan Thiagarajan

ASKER

Could you please give me a code for

deep clone


Shallow clone

A simple code to understand
public class Class1 : ICloneable
{
      public string Property1 { get; set; }
   
      public Class2 Class2Property { get; set; }

      public object Clone()
      {
            return new Class1()
            {
                  Property1 = this.Property1,
                  Class2Property = this.Class2Property.Clone()
            };
      }
}

public class Class2 : ICloneable
{
      public string Property2 { get; set; }
   
      public object Clone()
      {
            return new Class2() { Property2 = this.Property2 };
      }
}
Deep copy becomes important when an object has an association to another object such as in the example above (i.e. Class1 contains Class2). In a shallow copy, Class2Property would be set to this.Class2Property. In a deep copy, Class2Property would be set to this.Class2Property.Clone(). In a deep copy, you get a true copy of ALL objects. In a shallow copy, you get a true copy of only the outer objects.
Hi stmani2005,

Cloneable is an interface in Java. Its definition as follows:

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.

Note:- This interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

It throws the CloneNotSupportedException.