{"id":7310,"date":"2024-09-04T12:39:55","date_gmt":"2024-09-04T10:39:55","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7310"},"modified":"2025-01-14T13:59:17","modified_gmt":"2025-01-14T12:59:17","slug":"8-1-subclasses","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7310","title":{"rendered":"8.1 Subclasses"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7310&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=wpv2pages7310&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\">Let us start by looking at a possible description of two different types of accounts, savings accounts and credit accounts. We first show a class representing the notion of savings account. Some of the attributes are the same as in class <code>Account<\/code> from the previous chapters. In addition, new attributes and statements are added:<mark style=\"background-color:rgba(0, 0, 0, 0);color:#13990f\" class=\"has-inline-color\"> <\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> SavingsAccount(owner: <strong>ref<\/strong> Customer):\n   balance: <strong>var<\/strong> float\n   interest: <strong>var<\/strong> float\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>  releaseDate: <strong>var<\/strong> Date<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\">\n   <\/mark>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<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\">     <\/mark> 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\">Compared to class <code>Account<\/code> from the previous chapters, we have added a data-item <code>releaseDate<\/code> of type <code>Date<\/code>. It represents the date to which the customer has agreed to bind his\/her money in order to get a higher interest than on just an ordinary account.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The method <code>withdraw<\/code> differs from the one in class <code>Account<\/code>, since it is not possible to withdraw money before the release date.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have also added a method <code>newReleaseDate<\/code> with parameters <code>newDate<\/code> and <code>newInterest<\/code>. This method may set a new binding period and a new interest rate for a given a given savings account.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next let us look at similar description of a class representing the notion of credit account.<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> CreditAccount(owner: <strong>ref<\/strong> Customer):\n   balance: <strong>var<\/strong> float\n   interest: <strong>var<\/strong> float\n   maxCredit: <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):\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\">For a credit account it is possible to overdraw money up to a certain limit defined as the maximum limit. We have added a data-item <code>maxCredit<\/code>, that represents the maximum credit limit of the account.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We also have a new method <code>changeCredit<\/code>, which can set a new value for the maximum credit and a new interest rate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>withdraw<\/code> method is special in the sense that there is a test for not withdrawing beyond the <code>maxLimit<\/code>. We assume tha <code>maxLimit<\/code> is a positive float number. If <code>balance<\/code> is positive the customer has deposited money on the account. If <code>balance<\/code> is negative, the customer has loaned money from the bank.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, the two types of accounts have some attributes in common like <code>owner<\/code>, <code>balance<\/code>, <code>interest<\/code>, <code>addInterest<\/code>, and <code>deposit<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition, each of them have attributes that are specific for the kind of account they represent. For class <code>SavingsAccount<\/code> these are <code>releaseDate<\/code> and <code>newBindingPeriod<\/code>. For class <code>SavingsAccount<\/code> these are: <code>maxCredit<\/code> and <code>changeCredit<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next section, we show how to define the common attributes of the two classes in a single class that may be used to define these two classes.<\/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=7310\" 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>Let us start by looking at a possible description of two different types of accounts, savings accounts and credit accounts. We first show a class representing the notion of savings account. Some of the attributes are the same as in class Account from the previous chapters. In addition, new attributes and statements are added: Compared [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1008,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7310","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\/7310","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=7310"}],"version-history":[{"count":10,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7310\/revisions"}],"predecessor-version":[{"id":11125,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7310\/revisions\/11125"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1008"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}