Lock Own Classes
Having put a lot of effort in your own classes, you might wish to hide the functionality from inquisitorial looks. This is not a problem as long as you keep in mind that, once fully protected,
Mathematica does not allow to change the class. Assignments to fields are still possible. Let us look at an example to see how this works. First a "normal" set of classes is defined. Definitions are identical for all, the attributes are different and cover different levels of protection. The class
base will allow to check effects on inheritance below.
The obtainable information on the class is quite restricted, if the class has the attribute
ReadProtected.
On the other hand, other functions can be used to retrieve the definition.
Out[5]//Short= |
| |  |
Sealing the class' definition is easily achieved by means of the
Locked attribute.
The definitions cannot be read out anymore, if the symbol has attribute
ReadProtected.
Out[8]//Short= |
| |  |
Access to the class is almost not restricted. If a method (constructor included) and a global function have the same name and the function symbol has attribute
Locked, the attributes of the global function are used. Objects can be created, members used and fields reassigned.
| Out[9]= |  |
A locked class can still be a parent of another class. The next cell shows some examples.
| Out[10]= |  |
While the
base is locked, the
child may be changed. The next cell redefines the classes from above.
| Out[11]= |  |
If you do not change the patterns inside
base, you may redefine it. Pattern renaming is allowed, because it represents the same pattern.
| Out[13]= |  |
Any other redefinition of a parent class of a locked class causes complaints. Whenever a base class is redefined, the dependent classes are regenerated in order to be up to date. Because they are locked, this cannot be done.
Consequently, the derived classes are no longer usable.
| Out[15]= |  |