A slot's visibility is either private, protected, or public. If its visibility is not specified, the slot is private. The following example shows all four possibilities:
:- class example = [w:integer, private x:integer, protected y:integer, public z:integer]
Slot z
is public, y
is protected, and both x
and w
are
private.
Direct access to private slots is strictly limited to the methods of the class. Any other access to such slots must be accomplished through these methods. Making slots private will allow you later to change how you represent your class, adding and removing slots, without having to change any code that uses your class. You need only modify the methods of the class to accomodate that change. This is known as information hiding.
Protected slots are much like private slots, except that they can also be directly accessed by subclasses. This means that if you wish to modify the representation of your class, you will need to examine not only the class itself, but also its subclasses.
Public slots, in contrast, can be accessed from anywhere. This is
accomplished through automatically generated get and put methods named
for the slot and taking one argument. In the example above, our
example
class would automatically support a get and put method
named z/1
. Note, however, that unlike other object oriented
programming languages that support them, public slots in Quintus
Objects do not violate information hiding. This is because you may
easily replace a public slot with your own get and put methods of the
same name. In this sense, a public slot is really only a protected
slot with automatically generated methods to fetch and store its
contents.
Within a method clause, any of the class's slots can be accessed via
the fetch_slot/2
and store_slot/2
predicates. These are the
only way to access private and protected slots. They may be used to
define get and put methods for the class, which provide controlled
access to the protected slots. But, they can only be used within the
method clauses for the class, and they can only refer to slots of the
current class and protected and public slots of superclasses.
In the slot description, public
, protected
and private
are
used as prefix operators. The obj_decl
module redefines the prefix
operator public
, as follows:
:- op(600, fy, [public]).
Unless you use the obsolete public/1
directive in your Prolog
programs, this should cause no problems.