clone() has protected access - made public Object clone()
Asked Answered
T

3

27

I'm writing code to create an object, clone the object, then compare the two.

The object in question, Octagon, is an extension of an object GeometricObject

public class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable {
private double side;

public Octagon (double side){
    this.side = side;
}

public Object clone() throws CloneNotSupportedException {
    Octagon octClone = (Octagon)super.clone();
    return octClone;
}

In a file named Octagon.java

In another, TestOctagon.java, is my main method:

public class TestOctagon {
    public static void main(String[] args) {
        GeometricObject test = new Octagon(5); //create an Octagon with a side of 5
        System.out.println("Area is: "+test.getArea());
        System.out.println("Perimeter is: "+test.getPerimeter());

        Octagon copy = (Octagon)test.clone();


    }
}

The errors come in on the last line of the main method.

clone() has protected access in Object

I've tried renaming the clone method in Octagaon, say to cloneme, but then I get the error:

cannot find symbol
symbol: method cloneme()
location: variable test of type GeometricObject

I get the feeling the problem is because Octagon extends another object, maybe?

I really can't find any solution, and I've spent a good hour reading all the other clone() posts here.

Edit: It's required I use clone. I'm aware the general consensus is clone is borked.

Tola answered 16/4, 2013 at 18:55 Comment(1)
You can't override a protected method to be public. In your Octagon class define it as protected Object clone()Frijol
C
20

Replace

Octagon copy = (Octagon)test.clone();

with

Octagon copy = (Octagon)((Octagon)test).clone();

so that the called clone method is the one of your class.

Calcutta answered 16/4, 2013 at 18:58 Comment(5)
Exactly what was needed. Thanks much. I needed to use something similar to call the custom compareTo(Octagon o) function as well ((Octagon)test).compareTo(copy); Seems ridiculous this isn't mentioned anywhere in my textbook.Tola
clone is widely seen as badly designed, that's why you probably don't see it too much in textbooks.Phillisphilly
Unfortunately the assignment from the text specifically required the use of clone. =\ Interesting read though, thanks.Tola
hello, could you please explain why the need for a double cast?? I'm confused about it, thanx!Beabeach
@Beabeach The type of the test variable is Object. So it has to be casted in order to find the clone method. Then the returned type of clone is also Object, which means it has to be casted again to be assigned to the ̀copy` variable which is of type Object.Phillisphilly
N
9

You may write a copy-constructor:

public Octagon( Octagon right ){
    this.side = right.side;
}

And use it from the clone method:

public Object clone() throws CloneNotSupportedException {
    return new Octagon( this );
}
Naucratis answered 16/4, 2013 at 18:58 Comment(0)
M
0

I also faced the same error and solved it by creating one public function inside class

public class Octagon { ....

    public Octagon copy() {
        try {
          return (Octagon) clone();
        } catch(Exception e) {
          return new Octagon();
        } 
    }
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}
Melitta answered 27/9, 2023 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.