oop - What is the use of Cloning in JAVA and where it is frequently used? -


i have used programming , understood cloning means duplication of object. but, not idea in context , used? mean, appear used?

public abstract class geometricobject {      private string color = "white";     private boolean filled;      //default constructor     protected geometricobject(){      }     //constructing geometric object     protected geometricobject(string color, boolean filled){         this.color = color;         this.filled = filled;     }     //getter method color     public string getcolor(){         return color;     }     //set method color     public void setcolor(string color){         this.color = color;     }     //getter method filled     public boolean isfilled(){         return filled;     }     //setter method filled     public void setfilled(boolean filled){         this.filled = filled;     }     //abstract method getting area     public abstract double getarea();      //abstract method getting perimeter     public abstract double getperimeter();  }  public class octagon extends geometricobject implements comparable<octagon> , cloneable {     private double sides;     //default constructor     public octagon(){      }      /**new octagon object**/     public octagon(double side){         this.sides = side;     }      /**getter method**/     public double getside(){         return sides;     }      /**setter method**/     public void setside(double side){         this.sides = side;     }       @override     public double getarea() {         double area = (2*(1+(math.sqrt(2)))*sides*sides);         // todo auto-generated method stub         return area;     }      @override     public double getperimeter() {         double perimeter = sides * 8;         return perimeter;     }      @override     public int compareto(octagon o) {         if(getarea()>o.getarea())             return 1;         else if(getarea()<o.getarea())             return -1;         else              return 0;     }     @override      public object clone() {             try {               return super.clone();             }             catch (clonenotsupportedexception ex) {               return null;             }           } }  public class test {      public static void main(string[] args) {         /**creating new ocatgon object of having side value 5**/         octagon oct = new octagon(7);         //getting area of new octagon         system.out.println("the area of octagon side 5.0 (a): "+ oct.getarea());         /**getting perimeter of new octagon**/         system.out.println("the perimeter of octagon side 5.0 (p): "+ oct.getperimeter());         /*          * creating new object using clone method ,           * copy of oct side 5         */         octagon octagon1 = (octagon)oct.clone();          /*          * comparing 2 objects i.e.  using compareto method.          */         int i= oct.compareto(octagon1);         if(i<0){             system.out.println("clone octagon grater original octagon");         }else if(i>0)             system.out.println("clone octagon smaller original octagon");         else             system.out.println("clone octagon equal original octagon");        } } 

to clone object class should implement cloneable interface , should override clone method object class weird, clone method should have been on cloneable interface . implementation on clone method , using cloned object correct. assume not getting concept duplication of objects . example

you have object of octagon octagon octagon1 = new octagen();;

and have referrence of octagon octagon2 = octagon1;

even though octagon1 , octagon2 2 different variables keeping referrence on same object. if make change object using variable , change affect 2 variables since referring same object.

octagon2.setside(2); octagon1.getside(); //will return 2. 

by cloning objects won't end above problem.

 octagon oct = new octagon(7);  octagon octagon1 = (octagon)oct.clone();  octagon1.setside(3); oct.getside(); //will not return 3 because not referring same object. 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -