Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

package-accessible clone?

Thread view: 
Hendrik Maryns - 22 Dec 2005 18:58 GMT
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.
Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org

zero - 23 Dec 2005 18:24 GMT
> Hello,
>
[quoted text clipped - 37 lines]
>
> Thanks for some comments, H.

How about you just rename the clone method to copy()  That way the copy
method can have package access, and the compiler won't complain.  This
copy method could just return this.clone(), if that is appropriate for
your design.  Object:clone has protected access (which is also why the
compiler complains when you want to make it package access), so that's
consistent with your idea of not making it public.  If the design doesn't
call for a protected clone method, just put the cloning code in the copy
method directly.

--

Beware the False Authority Syndrome


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.