I am writing this little program but it has a mistake which I can't find. I have been searching and reading about it on the internet for hours but I keep getting the same error message: java: clone() has protected access in java.lang.Object. I am trying to implement the method clone() on Point so that after executing Main only c1 does moveTo. Could someone please tell me what is the problem and how do I solve it?
public class Main {
public static void main(String[] args) {
Point p = new Point(5,7);
Circle c1 = new Circle(p, 3);
Circle c2 = new Circle(p, 5);
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
c1.moveTo(2,3);
Circle cloned = (Circle) c2.clone();
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
} }
public class Circle {
public final Point center;
public final int radius;
public Circle(Point center, int radius) {
this.center = center;
this.radius = radius;
}
public void moveTo(int x, int y){
center.setX(x);
center.setY(y);
}
@Override
public String toString() {
return "Circle{" +
"center=" + center.toString() +
", radius=" + radius +
'}';
} }
public class Point implements Cloneable{
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
} }