i want to clone a given object.
if i do this
public class Something{
Object o; //set in the constructor
public Something(Object o){
this.o = o;}
public Something clone() throws CloneNotSupportedException{
Something temp = super.clone();
if (o instanceof Cloneable) //important part
temp.o = o.clone(); //important part
else temp.o = o;
}
}
this will not work becuase o.clone() is protected.
if i do this instead
if (o instanceof Cloneable) //important part
temp.o = ((Cloneable)o).clone(); //important part
it won't work either because Cloneable is an empty interface.
so how do i convince the compiler that you can clone o?
Cloneable
and implement theclone
method. – Gehlbachclone
has not been overriden? – Gehlbach