H
Hendrik Maryns
Hello,
I have a compositum of classes which represent a formula. When writing
down formulas, it is usual to define `predicates´: abbreviations for
long formulas, which may contain several free variables, which later
reappear with specific variables filled in.
To implement this, I defined a class FormulaTemplate, which stores a
Formula and an array of Variables. It defines a method
getFormula(Variable... inputVariables), which should return a copy of
the stored formula where the variables at specific places (given at
construction time) are replaced by the supplied variables.
To do this, I want to clone the stored formula (it should be possible to
invoke this method several times with different arguments), replace the
variables where needed and return the clone.
On the other hand, I want formulas to be immutable. All its methods are
inspector methods or return bigger formulas. So it does not make sense
to define a public clone method in Formula.
So I thought: put them in the same package, and make clone() package
accessible, then the Template has acces to it, whereas client classes do
not:
/**
* Creates and returns a copy of this object.
*
* @see java.lang.Object#clone()
*/
@Override
abstract Formula clone();
But now the compiler complains: Cannot reduce the visibility of the
inherited method from Object.
What is a good solution to this problem? Why can´t I declare clone()
package-accessible? Should I define a specific method for this? I
don´t want Formula to implement Cloneable either, as clients should not
know about clone().
Thanks for some comments, H.
--
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org
I have a compositum of classes which represent a formula. When writing
down formulas, it is usual to define `predicates´: abbreviations for
long formulas, which may contain several free variables, which later
reappear with specific variables filled in.
To implement this, I defined a class FormulaTemplate, which stores a
Formula and an array of Variables. It defines a method
getFormula(Variable... inputVariables), which should return a copy of
the stored formula where the variables at specific places (given at
construction time) are replaced by the supplied variables.
To do this, I want to clone the stored formula (it should be possible to
invoke this method several times with different arguments), replace the
variables where needed and return the clone.
On the other hand, I want formulas to be immutable. All its methods are
inspector methods or return bigger formulas. So it does not make sense
to define a public clone method in Formula.
So I thought: put them in the same package, and make clone() package
accessible, then the Template has acces to it, whereas client classes do
not:
/**
* Creates and returns a copy of this object.
*
* @see java.lang.Object#clone()
*/
@Override
abstract Formula clone();
But now the compiler complains: Cannot reduce the visibility of the
inherited method from Object.
What is a good solution to this problem? Why can´t I declare clone()
package-accessible? Should I define a specific method for this? I
don´t want Formula to implement Cloneable either, as clients should not
know about clone().
Thanks for some comments, H.
--
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org