Mandatory cloneable interface in Java -
i'm having small problem in java. have interface called modifiable. objects implementing interface modifiable.
i have modifycommand class (with command pattern) receive 2 modifiable objects (to swap them in list further on - that's not question, designed solution already).
the modifycommand class starts making clones of modifiable objects. logically, made modifiable interface extends cloneable. interface defines clone() method implementing classes must redefine.
then, in modifycommand, can : firstmodifiableobject.clone(). logic classes implementing modifiable have redefine clone method object, cloneable (that's want do).
the thing is, when define classes implements modifiable , want override clone(), won't let me, stating clone() method object class hides 1 modifiable.
what should do? i'm under impression "i'm doing wrong"...
thanks,
guillaume.
edit : think forget clone() thing. either a) assume object passed modifiable object (implementing interface) cloned or b) make method called, example, copy(), deep-copy of modifiable object (or maybe generic solution work...).
if you're using java 1.5 or higher, can behavior want , remove casting way:
public interface modifiable<t extends modifiable<t>> extends cloneable { t clone(); } public class foo implements modifiable<foo> { public foo clone() { //this required return null; //todo: real work } }
since foo extends object, still satisfies original contract of object class. code doesn't refine clone() method correctly not compile, because of additional constraints imposed modifiable interface. bonus, calling code doesn't have cast result of clone method.
Comments
Post a Comment