How to clone objects



This example shows how to clone objects by implementing the Cloneable interface. Some classes implements the Cloneable interface by default but when creating custom classes the interface needs to be implemented explicitly.
The class that implements the Cloneable interface (the Person class in the example) needs to have a method named clone() that returns an object.
In the example we have chosen to create a new instance of Person and populate its properties with the same values as the object that the clone() method
is called on.

Since we chose to populate the cloned object with the same values of its variables as the object that the clone() method was call on, the output of the above example will look like this:







Search for more Java info on this site here:
Custom Search


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">