{"id":7751,"date":"2024-09-27T11:20:43","date_gmt":"2024-09-27T09:20:43","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7751"},"modified":"2025-01-14T14:04:52","modified_gmt":"2025-01-14T13:04:52","slug":"8-1-1-common-superclass","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7751","title":{"rendered":"8.1.1 Common superclass"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7751&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=wpv2pages7751&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\">As mentioned, both types of accounts have a <code>withdraw<\/code> method, but the statement part of of these two methods are different.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may define the common attributes in a general class <code>Account<\/code> like the one we have defined in the previous chapters:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):\n   balance: <strong>var<\/strong> float\n   interest: <strong>var<\/strong> float\n   addInterest:\n      balance := balance + (balance * interestRate) \/ 100\n   deposit(amount: <strong>var<\/strong> float):\n      balance := balance + amount\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      -\"-\n      newB := balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We have included a partial description of a <code>withdraw<\/code> method to indicate that accounts in general have a <code>withdraw<\/code> method that returns the value of <code>balance<\/code>. In section <script>mkRef(\"Virtual methods\")<\/script> , we introduce a language mechanism called <em>virtual method<\/em> that makes it possible to describe such partial methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may the use class <code>Account<\/code> in the description of the more specific accounts. For class <code>SavingsAccount<\/code> this looks as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> SavingsAccount: Account\n   releaseDate: <strong>var<\/strong> Date\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float: \n      if (today &gt; releaseDate) :then\n         balance := balance - amount\n      :else\n         console.print(\"It is not possible to withdraw\")\n      newB := balance\n    newReleaseDate(newDate: <strong>var<\/strong> Date, newInterest: <strong>var<\/strong> float):\n      releaseDate := newDate\n      interest := newInterest<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>SavingsAccount<\/code> is described as a <em>subclass<\/em> of class <code>Account<\/code> &#8212; specified by <code>Account<\/code> following the <code>':'<\/code> (colon) in class <code>SavingsAccount<\/code>, like: <code><strong>class<\/strong> SavingsAccount: Account -\"-<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Making <code>SavingsAccount<\/code> a subclass of <code>Account<\/code> implies that all the attributes defined in class <code>Account<\/code> are also attributes of <code>SavingsAccount<\/code>. A <code>SavingsAccount<\/code> objects thus have attributes <code>owner<\/code>, <code>balance<\/code>, <code>interest<\/code> and <code>deposit<\/code>. Only the attributes special for a savings account need to be described in class <code>SavingsAccount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We note again, that a <code>withdraw<\/code> method is defined in both class <code>Account<\/code> and class <code>SavingsAccount<\/code> and we will show how to specify this using a virtual method in the next section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may make a similar description of class <code>CreditAccount<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> CreditAccount: Account\n   maxCredit: <strong>var<\/strong> float\n   withdraw(amount: <strong>var<\/strong> float):\n      if (-balance &lt; maxCredit) :then\n         balance := balance - amount\n      :else \n         console.print(\"Not possible to withdraw beyond max credit\") \n   changeCredit(newMax: <strong>var<\/strong> float, newInterest: <strong>var<\/strong> float):\n       maxCredit := newCredit\n       interest := newInterest <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>CreditAccount<\/code> is also described as a subclass of <code>Account<\/code> and only the attributes that are special for a credit account are specified.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We will say that <code>SavingsAccount<\/code> and <code>CreditAccount<\/code> are subclasses of <code>Account<\/code>, and that  <code>Account<\/code> is a <em>superclass<\/em> of <code>SavingsAccount<\/code> and <code>CreditAccount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The figure below illustrates the sub\/super-class relationships between the classes <code>Account<\/code>, <code>SavingsAccount<\/code> and <code>CreditAccount<\/code>. As used before, a rectangle with a yellow background represents a class. We do not list the attributes from a superclass in a subclass &#8211; only attributes added in the subclass are shown.<\/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<p class=\"wp-block-paragraph\">We use a diagram of this form to illustrate the realtionships between a class, its subclasses and possible superclass. See chapter <script>mkRef(\"Notation\")<\/script>.<\/p>\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 is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"507\" height=\"300\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/subclass.jpg\" alt=\"\" class=\"wp-image-9458\" style=\"width:369px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/subclass.jpg 507w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/subclass-300x178.jpg 300w\" sizes=\"(max-width: 507px) 100vw, 507px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>In general the term <em>superclass<\/em> refers to a class from which other classes are derived. <\/li>\n\n\n\n<li>The&nbsp;direct&nbsp;superclass is the class from which the class\/singular object is explicitly&nbsp;derived as specified in the object descriptor for the class\/singular object &#8211; see the extended definition of object descriptor in section <script>mkRef(\"Terminology: object-descriptor again\")<\/script>.<\/li>\n\n\n\n<li>The term <em>direct subclass<\/em> is an immediate subclass of a given class.<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Account<\/code> is a direct superclass of  <code>SavingsAccount<\/code> and <code>CreditAccount<\/code>, and these are both direct subclasses of <code>Account<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is in general possible to create as many subclasses as needed to represent a given concept classification hierarchy. In the above examples we may have several subclass of class <code>Account<\/code> and we may in turn have subclasses of <code>SavingsAccount<\/code> and <code>CreditAccount<\/code>. We give several examples of this in the following.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/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=7751\" 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>As mentioned, both types of accounts have a withdraw method, but the statement part of of these two methods are different. We may define the common attributes in a general class Account like the one we have defined in the previous chapters: We have included a partial description of a withdraw method to indicate that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7310,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7751","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\/7751","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=7751"}],"version-history":[{"count":19,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7751\/revisions"}],"predecessor-version":[{"id":11127,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7751\/revisions\/11127"}],"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=7751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}