[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cloneable

Posted on 2012-09-10
7
Medium Priority
?
327 Views
Last Modified: 2012-09-11
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
0
Comment
7 Comments
 
LVL 4

Expert Comment

by:fritzfrancis
ID: 38382878
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38382879
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
 
LVL 4

Expert Comment

by:fritzfrancis
ID: 38382883
Here is some code:

public class Class1 : ICloneable
{
      public string Property1 { get; set; }
   
      public object Clone()
      {
            return new Class1() { Property1 = this.Property1 };
      }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 38383232
Could you please give me a code for

deep clone


Shallow clone

A simple code to understand
0
 
LVL 4

Expert Comment

by:fritzfrancis
ID: 38383962
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
 
LVL 4

Expert Comment

by:fritzfrancis
ID: 38383977
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
 
LVL 1

Expert Comment

by:Siva Kumar Akurati
ID: 38386180
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question