JAVA DEEP CLONING PROGRAM

import java.io.Serializable;
class Ant implements Serializable,Cloneable{
       public String sms;
       public Ant(String name){
              this.sms = “Help me ,”+ name +” is eating me”;
       }

       public void printSMS(){
              System.out.println(“Ant SMS : “+sms);
       }

       public Ant clone() throws CloneNotSupportedException{
              return (Ant)super.clone();
       }
}
class Bird implements Serializable,Cloneable{
       public Ant ant = null;
       public String name;
       public String sms;

       public Bird(String name){
              ant = new Ant(name);
              this.name = name;
              this.sms = “Welcome to “+ name +” World”;
       }
       public void printSMS(){
              System.out.println(“Bird SMS : “+sms);
       }

       public Ant getAnt(){
              return this.ant;
       }

       @Override
       public Bird clone() throws CloneNotSupportedException{
       Bird cloneBird = (Bird)super.clone();
              cloneBird.ant = ant.clone();
              return cloneBird;
       }
}

class CloneFlight {

       public static void main(String[] args) {
       System.out.println(“—-Bird1 detail—–“);
       Bird bird1 = new Bird(“Sparrow”);
       bird1.printSMS();
       bird1.getAnt().printSMS();
       try{
              System.out.println(“\n\n —-Bird2 detail—–“);
              Bird bird2 = bird1.clone();
              bird2.printSMS();
              bird2.getAnt().printSMS();
              bird1.getAnt().sms = “please”;
              bird1.getAnt().printSMS();
              bird2.getAnt().printSMS();
              bird1.sms=”hi”;
              bird2.sms=”hello”;
              bird1.printSMS();
              bird2.printSMS();
       }catch(Exception ex){
              System.out.println(“Error : “+ex.getMessage());
       }
       }
}

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in Uncategorized. Bookmark the permalink.

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