English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm using Java 1.4.1 ; I'm trying to something like this, I get an error that says clone() is not defined for Cloneable...



public class DeepCloneableVector extends Vector implements Cloneable{

public Object clone()
{
DeepCloneableVector v = (DeepCloneableVector)super.clone();
int size = this.size();

for (int i=0; i {
Cloneable element = (Cloneable)get(i); //get element in this vector

//set the element in the next vector
v.setElementAt(element.clone(), i);


}
return v;
}
}

2006-06-24 06:12:04 · 2 answers · asked by shellsandscripts 2 in Computers & Internet Programming & Design

2 answers

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.

Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

That means you cannot simply say implements Cloneable since Cloneable has absolutely no methods. Especially not clone. The Object Interface does have clone. Which has the structure of: protected Object clone() throws CloneNotSupportedException

As the docs say:
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

2006-06-24 07:04:08 · answer #1 · answered by ? 6 · 1 1

The "implements" keyword means that you're implementing an interface, which means you're writing a class, which looks like "class CalendarAppointment implements Appointment". The "extends" keyword means that you're defining a child class from a parent class, such as "class Student extends Person".

2016-03-27 03:05:25 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers