why can't we just extend the class that will be using the clone method in order to be able to use it without overiding?
The clone() method
Collapse
X
-
-
Originally posted by momotarothe same message clone is protected
you have to override it (and call the superclass's implementation) and
occasionally you have to implement the Cloneable marker interface as well
(if that hasn't been done so already in a superclass).
kind regards,
JosComment
-
-
Originally posted by momotarobut when we have a class inhereting from another if the superclass members are protected they are visible for the subclass! why its not the same thing for method clone!?
clone() method checks whether or not a particular class (or a parent thereof)
implements the Cloneable interface.
Suppose my father knows a famous protected beer brewing recipe. I can ask
him to brew beer but you can't ask me to brew beer although my father knows
how to do it.
kind regards,
JosComment
-
Originally posted by MomotaroSo why am having that error in line 2 :
illigal start of expretion not astatement ';' expected
throws CloneNotSupport edException {
CloneThatObject originalObject =
new CloneThatObject ("toto", "foo");
CloneThatObject copyObject = (CloneThatObjec t)
originalObject. clone();
}
}[/CODE]Comment
-
A demo an a few comments.
[CODE=Java]public class Example implements Cloneable {
private String text;
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
public Example(String text) {
setText(text);
}
@Override()
public String toString() {
return getText();
}
@Override()
public Example clone() {
try {
return (Example) super.clone();
} catch (CloneNotSuppor tedException e) {
throw new Error(e);
}
}
public static void main(String[] args) {
Example a = new Example("hello world");
Example b = a.clone();
System.out.prin tln(b);
}
}[/CODE]
1. I claim that most of the time (check your design) if a class has a public clone, it's intended that it and all its subclass will not throw CloneNotSupport edException, so I struck that from the method's signature.
2. Don't forget about covariant return types: http://www.java-tips.org/java-se-tip...urn-types.htmlComment
-
Originally posted by momotaroand how to hide the clone method from a class that extends a class implementing cloneable?Comment
Comment