Note that there are some explanatory texts on larger screens.

plurals
  1. POclone() has protected access - made public Object clone()
    text
    copied!<p>I'm writing code to create an object, clone the object, then compare the two.</p> <p>The object in question, Octagon, is an extension of an object GeometricObject</p> <pre><code>public class Octagon extends GeometricObject implements Comparable&lt;Octagon&gt;, Cloneable { private double side; public Octagon (double side){ this.side = side; } public Object clone() throws CloneNotSupportedException { Octagon octClone = (Octagon)super.clone(); return octClone; } </code></pre> <p>In a file named Octagon.java</p> <p>In another, TestOctagon.java, is my main method:</p> <pre><code>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(); } } </code></pre> <p>The errors come in on the last line of the main method.</p> <pre><code>clone() has protected access in Object </code></pre> <p>I've tried renaming the clone method in Octagaon, say to cloneme, but then I get the error:</p> <pre><code>cannot find symbol symbol: method cloneme() location: variable test of type GeometricObject </code></pre> <p>I get the feeling the problem is because Octagon extends another object, maybe?</p> <p>I really can't find any solution, and I've spent a good hour reading all the other clone() posts here.</p> <p>Edit: It's required I use clone. I'm aware the general consensus is clone is borked.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload