Accessibility Modifiers?
Table of Contents
Concurnas supports four key accessibility modifiers (in order of restrictability): private, package, protected and public
. These are essential for supporting encapsulation from an object oriented programming perspective. Making diligent use of accessibility modifiers improves code readability (by clearly marking the accessibility of code), reduces maintenance costs of software (e.g. one can change the argument structure of a private function knowing that the only callers of said function are local to the definition), and generally leads to more structured, thought out code being written.
Modifiers can be applied at either the top level module level or more commonly to class elements (fields and methods). In total the following 8 types of referable/importable item can have accessibility modifiers: variables, class fields, functions, class methods, classes, enumerations, typedefs, annotations. The accessibility modifier keyword is placed first in the definition of said element. e.g.
//at the module level...
private raiser= 99
public mypow(a int) = a ** raiser
//at the class level...
class MyClass{
private age int = 20
public isOldEnough(test int) = test >== age
}
In the above example, the private variables and fields are not accessible outside of their defined module and class respectfully. However the publicly accessible function and methods are. private
, public
and protected
are the most commonly used accessibility modifiers. A full table showing which items are assessable from what other parts of the code is as follows:
Accessible from |
|
|
|
|
---|---|---|---|---|
Same Class/Module |
Y |
Y |
Y |
Y |
Subclass |
Y |
Y |
N |
N |
Package |
Y |
Y |
Y |
N |
Global |
Y |
N |
N |
N |
Default Accessibility?
All of the aforementioned accessibility modifiable elements have a default accessibility applied where no accessibility modifier is specified. For example in the below case:
class MyClass{
age int = 20
isOldEnough(test int) = test >== age
}
The age
field has a default accessibility applied of private and the isOldEnough
method has a default accessibility applied of public. A table showing the default accessibility for each of the items, contingent on their defined context is as follows:
Item |
Default |
---|---|
variable |
|
field |
|
function |
|
method |
|
class |
|
nested class |
|
enumeration |
|
annotation |
|
typedef |
|