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
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
 
käµfm³d 👽Commented:
cloneable means it would create an copy of Insance.
This depends on your definition of "copy", I think. Depending on how the author of the method implementation writes the method, you may get either a "deep copy" (true copy) or a "shallow copy" (copy of references).

A deep copy contains a copy of all the data contained within the original object. This deep copy exists entirely on its own and is independent of the original (think of the cloned sheep a few years back).

A shallow copy is a new reference to existing data. While you will have a new variable pointing to the original's data, any changes to that data will be seen by both the original and the clone.
0
 
fritzfrancisCommented:
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.
0
 
fritzfrancisCommented:
Here is some code:

public class Class1 : ICloneable
{
      public string Property1 { get; set; }
   
      public object Clone()
      {
            return new Class1() { Property1 = this.Property1 };
      }
}
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Manikandan ThiagarajanSenior consultantAuthor Commented:
Could you please give me a code for

deep clone


Shallow clone

A simple code to understand
0
 
fritzfrancisCommented:
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 };
      }
}
0
 
fritzfrancisCommented:
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.
0
 
Siva Kumar AkuratiSenior Software EngineerCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.