简体   繁体   中英

Implementing clone method - Error java: clone() has protected access in java.lang.Object

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 +
                '}';
    } }

It is because the line Circle cloned = (Circle) c2.clone(); You are trying to access a protected member outside of the class it is defined. A protected method can only be called in the same class or a child class.

if you really want to access it in the Main class only, Then override the clone() method in Circle class and make it public as well as write your own implementation if needed.

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 Object clone() throws CloneNotSupportedException {
        // TODO: Your custom clone logic
        return super.clone();
    }

    @Override
    public String toString() {
        return "Circle{" +
            "center=" + center.toString() +
            ", radius=" + radius +
            '}';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM