Clone() method SDK 1.4

M

michael

To newsgroup,

<code snippet>

package folder;
import folder.Bar;

class Foo{
public static void main (String[] args){
Bar b = new Bar();
Object obj = b.clone();
Object[] array = new Object[10];
array[0] = obj;
}
}

.....
package folder;

class Bar implements Cloneable{
public Bar(){ System.out.println("creating bar object");}
}


Problem: I get a compile-time error complaining that the clone() method is
package protected. As you can see I have packaged both classes in the same
package, and therefore I should not be getting this error. What am i doing
wrong? note, I am using Java SDK 1.4


Any help appreciated

Regards
programmer
 
T

Thomas Fritsch

michael said:
To newsgroup,

<code snippet>

package folder;
import folder.Bar;

class Foo{
public static void main (String[] args){
Bar b = new Bar();
Object obj = b.clone();
Object[] array = new Object[10];
array[0] = obj;
}
}

....
package folder;

class Bar implements Cloneable{
public Bar(){ System.out.println("creating bar object");}
}


Problem: I get a compile-time error complaining that the clone() method is
package protected. As you can see I have packaged both classes in the same
package, and therefore I should not be getting this error. What am i doing
wrong? note, I am using Java SDK 1.4
You need a public clone method. Simply add the following to your Bar class
public Object clone() { return super.clone(); }
 
T

Thomas Fritsch

Thomas said:
You need a public clone method. Simply add the following to your Bar
class
public Object clone() { return super.clone(); }

Oops, I meant
public Object clone() throws CloneNotSupportedException { return
super.clone(); }
 
J

John C. Bollinger

michael said:
Problem: I get a compile-time error complaining that the clone() method is
package protected. As you can see I have packaged both classes in the same
package, and therefore I should not be getting this error.

The clone() you are attempting to invoke is a protected member of class
java.lang.Object. Although it is therefore inherited by all other
classes, its scope is still determined by the class in which it is
declared. Override it with a public or another protected version on
your own class (which may simply delegate to Object's version) to gain
access from other classes.


John Bollinger
(e-mail address removed)
 
R

Roedy Green

The clone() you are attempting to invoke is a protected member of class
java.lang.Object. Although it is therefore inherited by all other
classes, its scope is still determined by the class in which it is
declared. Override it with a public or another protected version on
your own class (which may simply delegate to Object's version) to gain
access from other classes.

for details of how see http://mindprod.com/jgloss/clone.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top