A method can be either public or private. Any object can send a message that runs a public method. A private method can only be invoked from specific calling contexts. These contexts are:
::class Savings |
::method newCheckingAccount CLASS |
instance = self~new |
instance~makeChecking |
return instance |
::method makeChecking private |
expose checking |
checking = .true |
The newCheckingAccount CLASS method is able to invoke the makeChecking method because the scope of the makeChecking method is .Savings.
::class Savings |
::method init class |
expose counter |
counter = 0 |
::method allocateAccountNumber private class |
expose counter |
counter = counter + 1 |
return counter |
::method init |
expose accountNumber |
accountNumber = self~class~allocateAccountNumber |
The instance INIT method of the Savings class is able to invoke the allocateAccountNumber private method of the .Savings class object because it is owned by an instance of the .Savings class.
Private methods include methods at different scopes within the same object. This allows superclasses to make methods available to their subclasses while hiding those methods from other objects. A private method is like an internal subroutine. It shields the internal information of an object to outsiders, but allowing objects to share information with each other and their defining classes.