I have used programming and understood that cloning means the duplication of a object. But, I could not get idea about in what context and where it is mainly used? I mean, where does it appear to be used?
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
//Default constructor
protected GeometricObject(){
}
//Constructing a Geometric object
protected GeometricObject(String color, boolean filled){
this.color = color;
this.filled = filled;
}
//Getter method for color
public String getColor(){
return color;
}
//set method for color
public void setColor(String color){
this.color = color;
}
//Getter method for filled
public boolean isFilled(){
return filled;
}
//setter method for filled
public void setFilled(boolean filled){
this.filled = filled;
}
//Abstract method for getting Area
public abstract double getArea();
//Abstract method for 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 a 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 with side 5.0 is (A): "+ oct.getArea());
/**Getting perimeter of new Octagon**/
System.out.println("The perimeter of Octagon with side 5.0 is (P): "+ oct.getPerimeter());
/*
* Creating a new object using clone method and
* copy of oct whose side is 5
*/
Octagon octagon1 = (Octagon)oct.clone();
/*
* comparing the two objects i.e. using compareTo method.
*/
int i= oct.compareTo(octagon1);
if(i<0){
System.out.println("Clone Octagon is grater than original octagon");
}else if(i>0)
System.out.println("Clone octagon is smaller than original octagon");
else
System.out.println("Clone octagon is Equal to original octagon");
}
}
to clone
an object
your class should implement Cloneable
interface and you should override clone
method from object class which is weird, clone method should have been on the Cloneable interface . your implementation on Clone method and using the cloned object is correct. i assume you are not getting the concept Duplication of objects
. for an example
you have an object of octagon Octagon octagon1 = new Octagen();;
and you have a referrence of Octagon octagon2 = octagon1;
even though octagon1
and octagon2
are 2 different variables they are keeping referrence on the same object. if you make a change to the object using any variable , the change will affect the 2 variables since they referring to the same object.
octagon2.setSide(2);
octagon1.getSide(); //will return 2.
by cloning the objects you won't end up with above problem.
Octagon oct = new Octagon(7);
Octagon octagon1 = (Octagon)oct.clone();
octagon1.setSide(3);
oct.getSide(); //will not return 3 because they are not referring to the same object.