{"id":10455,"date":"2024-12-15T12:15:31","date_gmt":"2024-12-15T11:15:31","guid":{"rendered":"https:\/\/oopm.org\/?page_id=10455"},"modified":"2025-01-24T11:25:13","modified_gmt":"2025-01-24T10:25:13","slug":"14-1-visibility-mechanisms","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=10455","title":{"rendered":"14.1 Visibility mechanisms"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages10455&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages10455&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Here we introduce some language mechanisms that may be used to separate the representative elements from the non-representative elements. The language elements are called <em>access modifiers<\/em> and they may specify the accessibility of attributes of an object. Access modifiers define the scope and visibility of these attributes in the code. This means that the programmer may specify to what extent a local data-item, method or class may be accessed in other parts of the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The most common types of access modifiers are <em>public<\/em>, <em>private<\/em>, and <em>protected<\/em>, but some languages support more as is the case for qBeta (see below). Each access modifier provides different levels of access. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">%private<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As a first example, we will show how to use the access modifier <code>%private <\/code>to protect <code>balance<\/code> and <code>interestRate<\/code> of <code>Account<\/code> objects:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account(owner: <strong>var<\/strong> String):\n   addInterest: -\"-\n   deposit(amount: <strong>var<\/strong> float): -\"-\n   withdraw(amount: var float) -&gt; newB: <strong>var<\/strong> float: -\"-\n   getBalance -&gt; bal:&nbsp;<strong>var<\/strong>&nbsp;float: -\"-\n   getInterestRate -&gt; iRate: <strong>var<\/strong> float: ...\n   setInterestRate(iRate: <strong>var<\/strong> float): ...\n   %private\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The access modifier <code>%private<\/code> before the declarations of <code>balance<\/code> and <code>interestRate<\/code> specfies that <code>balance<\/code> and <code>interestRate<\/code> may only be accessed within class <code>Account<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"530\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/AccountWithPrivate-1024x530.jpg\" alt=\"\" class=\"wp-image-9584\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/AccountWithPrivate-1024x530.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/AccountWithPrivate-300x155.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/AccountWithPrivate-768x397.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/AccountWithPrivate.jpg 1077w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Representative and non-representative parts of models in the bank domain<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">%protected<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When using <code>%private<\/code> in class <code>Account<\/code> as above, no other classes or objects may acces <code>balance<\/code> and <code>interestRate<\/code>. However, this may be inconvenient for subclasses of <code>Account<\/code> like <code>SavingsAccount<\/code>, where we may add methods that manipulate these attributes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The access modifier <code>%protected <\/code>may be used to specify that some attributes may only be accessed within a class and its subclasses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may thus revise class <code>Account<\/code> as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account:\n   -\"-\n   %protected\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float\n<strong>class<\/strong> SavingsAccout: Account\n   -- balance and interestRate may now be accessed here\n   -\"-\naClerk: <strong>obj<\/strong>\n   -- balance and interestRate may not be accessed\n   -\"-<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">%public<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For completeness, the access modifier <code>%public<\/code> may be used to specify that some attributes are visible outside the object-descriptor. However, <code>%public<\/code> is <em>default<\/em> in qBeta &#8211; if no access modifier is specified for some attributes they are visible outside the object-descriptor. Sometimes it may be useful to use <code>%public<\/code> if one prefer to have e.g. the private attributes appearing before the ones that are public. This is the case for the version of class <code>Account<\/code> below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account:\n   %protected \n   balance:&nbsp;<strong>var<\/strong>&nbsp;float\n   interestRate:&nbsp;<strong>var<\/strong>&nbsp;float\n   %public\n   addInterest: -\"-\n   -\"-<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>balance<\/code> and <code>interestRate<\/code> are declared in the beginning as <code>%protected<\/code> followed by the methods of the class &#8211; these are thus explicitly declared as <code>%public<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">%package_boundary and %package<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This section is a reference section, which may be skipped during a first reading. So far we have no examples using the package access modifiers, but one will be given in section <script>mkRef(\"Modules\")<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A <em>package<\/em> is a self-contained collection of modules that consist of a top module with local modules where each local module may contain local modules, etc. The access modifier <code>%package_boundary<\/code> defines the top module of the package &#8211; being the module where <code>%package_boundary<\/code> is placed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The access modifier <code>%package<\/code> may be used to specify that some attributes are only visible in modules within the current package where the current package is defined by the nearest enclosing module with a <code>%package_boundary<\/code> modifier and its local modules, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary of access modifiers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this section we summarize the description of  access modifiers &#8211; we use the following sketchy example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> T:\n   ...\n   %public\n   x: <strong>var<\/strong> integer\n   m(...):\n      ...\n   %private\n   y: <strong>var<\/strong> integer\n   l: <strong>obj<\/strong> Set(integer)\n   ...\n   %protected\n   z: <strong>var<\/strong> integer\n   s: <strong>ref<\/strong> Person\n   ...\n   %domain\n   v: <strong>var<\/strong> real\n   foo:\n      ...\n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class <code>T<\/code> is specified using the access modifiers <code>%public<\/code>, <code>%private,<\/code>  <code>%protected<\/code>, and <code>%domain<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An access modifier applies to all the attributes following the modifier, up to the next access modifier. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next example shows a usage of class <code>T<\/code> where we have a <code>T<\/code>-object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>r: <strong>obj<\/strong> T\n-- The access modifiers specify if we may acces the attributes\n-- r.x, r.y, r.z, r.v, ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the next example, we have a subclass <code>TT<\/code> of <code>T<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> TT: T\n   -- Here the access modifiers above also specify if we may access\n   -- x, y, z, v, ...<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Public<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The access modifier <code>%public<\/code> specifies that one or more attributes are accessible from outside the object-descriptor where it is declared. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the above examples <code>x<\/code> and <code>m<\/code> may be accessed via <code>r.x<\/code>, <code>r.m(...)<\/code> and as <code>x<\/code> an <code>m(...)<\/code> in the subclass <code>TT<\/code> of <code>T<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Private<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The access modifier<code> %private <\/code>specifies that one or more attributes may only be accessed within the object-descriptor where they are declared. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This means that <code>r.<\/code>y and <code>r.l<\/code> are not legal and that <code>y<\/code> and <code>l<\/code> may not be accessed in the subclass <code>TT<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Protected<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The access modifier <code>%protected<\/code> is like <code>%private<\/code> except that the attributes may be accessed in subclasses of the class where they are declared.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This means that <code>z<\/code> and <code>s<\/code> may not be accessed via <code>r.z<\/code> and <code>r.s<\/code> but may be accessed in all subclasses of <code>T<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Package_boundary and package<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The access modifier <code>%package_boundary <\/code>specifies the top module of a package and <code>%package<\/code> specifies that one or more attributes are visible in the enclosing package.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code><code>TopModule: <strong>obj<\/strong>\n   %package_boundary\n   ...\n   Module_A: <strong>obj<\/strong>\n      ...\n      %package\n      foo: ...\n      ...\n   Module_B:&nbsp;<strong>obj<\/strong>\n      ...\n      %package\n      bar: ...\n      ... <\/code><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"358\" height=\"269\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ModuleStructure.jpg\" alt=\"\" class=\"wp-image-10609\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ModuleStructure.jpg 358w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ModuleStructure-300x225.jpg 300w\" sizes=\"(max-width: 358px) 100vw, 358px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><br>The attributes <code>foo<\/code>\u00a0and\u00a0<code>bar<\/code>\u00a0are visible in <code>TopModule<\/code>,\u00a0<code>Module_A<\/code>,\u00a0and\u00a0<code>Module_B<\/code>.<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Default<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If no access modifier is specified, the default is <code>%public<\/code>.<\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=10455\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Here we introduce some language mechanisms that may be used to separate the representative elements from the non-representative elements. The language elements are called access modifiers and they may specify the accessibility of attributes of an object. Access modifiers define the scope and visibility of these attributes in the code. This means that the programmer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2250,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-10455","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/10455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10455"}],"version-history":[{"count":11,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/10455\/revisions"}],"predecessor-version":[{"id":11311,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/10455\/revisions\/11311"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2250"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}