JavaTip : Basic cloning example code in java

package com.premseem.cloning;

import java.util.Date;

public class CloningMain {

    public static void main(String[] args) throws CloneNotSupportedException {
        Employee empBreeder = new Employee(“aseem” , “Jain”);
        System.out.println(empBreeder);
        Employee empClone1 = (Employee) empBreeder.clone();
        Employee empClone2 = (Employee) empBreeder.clone();
        System.out.println(empClone1);
        System.out.println(empClone2);
    }
}

class Employee implements Cloneable{
   
    public Employee(String name, String surName){
        this.name = name;
        this.surname = surName;
    }
   
    String name;
    String surname;
    Date date = new Date();
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    @Override
    public String toString() {
        return name + ” ” + surname + ” ” + date +” ” + hashCode();
    }
}

 

Clone (κλών) is a Greek word meaning “branch”, referring to the process whereby a new plant can be created from a twig. In biology it is about copying the DNAs.In java, though clone is ‘intended’ to produce a copy of the same object it is not guaranteed. Clone comes with lots of its and buts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s