{"id":7753,"date":"2024-09-27T11:21:43","date_gmt":"2024-09-27T09:21:43","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7753"},"modified":"2025-01-14T14:11:22","modified_gmt":"2025-01-14T13:11:22","slug":"8-1-2-extended-type-rule","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7753","title":{"rendered":"8.1.2 Extended type rule"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7753&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=wpv2pages7753&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\">Subclasses makes it possible to organize classes in a hierarchy where common attributes are defined in a superclass and more specialised attributes in subclasses. In addition, it is possible to write code that is general for objects being instances of different classes and\/or subclasses in a hierarchy. For the bank example we may write code that works for all kinds of accounts. In order to be able to do this, we extend the type rule described in section <script>mkRef(\"Reference data-items\")<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider the example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anAccount: <strong>ref<\/strong> Account\naSavingsaccount: <strong>ref<\/strong> SavingsAccount\naCreditAccount : <strong>ref<\/strong> CreditAccount\naSavingsAccount := SavingsAccount(JohnSmithProfile)\naCreditAccount := CreditAccount(JohnSmithProfile)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the example above we create objects of type <code>SavingsAccount<\/code> and <code>CreditAccount<\/code> and assign the references to these objects to the reference variables <code>aSavingsAccount<\/code> and <code>aCreditAccount<\/code> respectively. This all in accordance with the type rule described in section <script>mkRef(\"Reference data-items\")<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may extend the type rule as shown in the next example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anAccount := aSavingsAccount\nanAccount.deposit(400)\nanAccount := aCreditAccount\nandAccount.withdraw(299)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We assign the reference held by a <code>aSavingsAccount<\/code> to the reference variable <code>anAccount<\/code>. We then invoke the <code>deposit<\/code> method using <code>anAccount<\/code>. This is possible since the <code>deposit<\/code> method is described in class <code>Account<\/code> and all attributes of <code>Account<\/code> are also attributes of class <code>SavingsAccount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may in a similar way assign the reference held by <code>aCreditAccount<\/code> to <code>anAccount<\/code> and subsequently invoke an <code>Account<\/code> method &#8211; here <code>withdraw<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In general the extended type rule is defined as follows<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">A reference expression of type <code>TT<\/code> may be assigned to a reference variable of type <code>T<\/code> if <code>T<\/code> is a (direct or indirect) superclass of <code>TT<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">If a reference variable <code>r<\/code> is of type <code>T<\/code> all attributes defined in class <code>T<\/code> may be accessed, but even if <code>r<\/code> refers to an object of type <code>TT<\/code>, attributes defined in class <code>TT<\/code> cannot be accessed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the account objects this means that using <code>anAccount<\/code> we may access the attributes defined in class <code>Account<\/code>. We may not access the ones defined in class <code>SavingsAccount<\/code> or <code>CreditAccount<\/code>, since we in general do not know if <code>anAccount<\/code> refers to an instance of <code>Account<\/code>,  <code>SavingsAccount<\/code> or <code>CreditAccount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may also assign a reference to a singular object derived from a class (e.g.<code> Account<\/code>) to a reference variable typed by this class (here <code>Account<\/code>) as shown in the following ghost example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anAccount: <strong>ref<\/strong> Account\naSpecialAccount: <strong>obj<\/strong> Account\n   withdraw::&lt;\n       -- create alert\n...\nanAccount := aSpecialAccount<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, <code>aSpecialAccount<\/code> may be assigned to <code>anAccount<\/code> which is of type <code>Account<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The rationale for the extended type rule is the same as for the simple type rule described in section <script>mkRef(\"Reference data-items\")<\/script>: it makes sense from a programming and modeling point of view and the compiler may check the rule and thus prevent errors of this kind a run-time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is however, possible to write a so-called <em>reverse assignment <\/em>like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aSavingAccount := anAccount<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here the compiler will check during execution of the program (at run-time) that <code>anAccount<\/code> does refer to an object of type <code>SavingsAccount<\/code>. The programmer may know that this is the case, but the compiler in general does not know.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One may discuss whether or not this is good from a programming and modeling point of view &#8211; with respect to safety, a run-time error may happen here.<\/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=7753\" 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>Subclasses makes it possible to organize classes in a hierarchy where common attributes are defined in a superclass and more specialised attributes in subclasses. In addition, it is possible to write code that is general for objects being instances of different classes and\/or subclasses in a hierarchy. For the bank example we may write code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7310,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7753","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\/7753","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=7753"}],"version-history":[{"count":15,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7753\/revisions"}],"predecessor-version":[{"id":11129,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7753\/revisions\/11129"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7310"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}