Re: Re: Constructors?
Daniel Bonniot <[email protected]> Tue, 12 Oct 2004 10:18:38 +0200
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
> That brings up a question though, how might you "privatize" base class
> constructors for a sub-class if you did not want any base class
> constructors called from the sub-class?
You can use visibility modifiers to restrict the visibility of some constructors.
> interface PropertyProvider
> {
> ?String get(String key);
> Collection getKeys();
> }
>
> class PropertyCache
> {
> private Map<String, String> map = new HashMap();
> private PropertyProvider provider;
> ?String get(String key) // Returns value associated with key.
> {if key not in hash then get key from provider and place in hash}
> }
>
> Ok, so here is somewhat of a real life setup. PropertyProvider defines
> an interface for looking up a value associated with a property key. The
> implementation may be a file, database, remote server, whatever. The
> PropertyCache provides an interface for the PropertyProvider so that
> properties are only queried from the PropertyProvider once, and stored
> locally. (In reality PropertyCache would probably implement
> PropertyProvider)
>
> Now, It may be the case that I want to "override" the existing
> this(provider:) constructor to modify behavior. For example I want to
> not use HashMap but some other type of map that is faster for a small
> number of keys. Or I want to load up all key/value pairs from the
> provider all at once and store them in the hash. The initializer is not
> the appropriate place for this because I may not want all constructors
> to have this behavior.
You could drop the default value for map and define a custom constructor:
new PropertyCache(PropertyProvider provider) {
...;
this(provider: provider, map: ...);
}
We could also decide that when a custom constructor has the same signature as
the default constructor, the default one is not generated. That would give
more flexibility. That could more easily be done once it's possible to set
individual fields in custom constructors (until then we always need a default
constructor to do the real setting).
Daniel
-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl