{"id":1043,"date":"2023-09-08T14:43:16","date_gmt":"2023-09-08T12:43:16","guid":{"rendered":"https:\/\/oopm.org\/?page_id=1043"},"modified":"2024-12-15T09:33:49","modified_gmt":"2024-12-15T08:33:49","slug":"5-1-virtual-methods","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=1043","title":{"rendered":"8.3 Virtual methods"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1043&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=wpv2pages1043&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\" id=\"block-4195ba60-5c74-465a-8396-6d372c2d46ca\">In this section, we will introduce virtual methods. Virtual methods may be used to specify partial methods that may be further extended in subclasses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-d56a8936-32cf-4204-858c-a419abe5f965\">In the next example, we define <code>withdraw<\/code> of <code>Account<\/code> as a virtual method:<\/p>\n\n\n\n<pre id=\"block-7ac7c72e-71b5-4e19-853e-1c6b1efbe1e9\" class=\"wp-block-code\"><code><strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):\n   ...\n   witdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:&lt;\n      inner(witdraw)<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>\n      newB := balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5d6e04ae-f4a1-42f4-8c7a-a5ecbcaad5ff\">The symbol <code>':&lt;'<\/code> specifies that <code>withdraw<\/code> is a virtual method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-347ecbf2-6a1d-4851-9450-badf0fb13fbd\">The statement part of <code>withdraw<\/code> consists of <code>inner<\/code> followed by <code>newB := balance<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5840c072-b007-4d1d-9345-c5cc7c41a74e\">The statement <code>inner<\/code> specifies that when executed, possible statements in a further extension of withdraw in a subclass of <code>Account<\/code> will be executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-ff38ed33-53cd-4f9a-9d3b-175f1d555162\">In the next example, we show how to extend <code>withdraw<\/code> in a class <code>SavingsAccount<\/code>:<\/p>\n\n\n\n<pre id=\"block-06472f4d-55d4-417b-8ce2-73b4e6f788da\" class=\"wp-block-code\"><code><strong>class<\/strong> SavingsAccount: Account\n   ...\n   withdraw ::\n      if (today &gt; releaseDate) :then\n         balance:= balance - amount\n      :else\n         console.print(\"It is not possible to withdraw\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-aeaa1b29-4f17-4bfb-8008-0660338f2378\">The symbol <code>'::'<\/code> specifies that <code>withdraw<\/code> is an <em>extension<\/em> of <code>withdraw<\/code> in the superclass <code>Account<\/code> of <code>SavingsAccount<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"462\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ExtendingInTwoSubclassesOfAccount-1024x462.jpg\" alt=\"\" class=\"wp-image-10434\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ExtendingInTwoSubclassesOfAccount-1024x462.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ExtendingInTwoSubclassesOfAccount-300x135.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ExtendingInTwoSubclassesOfAccount-768x346.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/ExtendingInTwoSubclassesOfAccount.jpg 1122w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-91ff1a34-d9d1-448c-92f0-4684461c359d\">In the following example, we invoke <code>withdraw<\/code> on a <code>SavingsAccount<\/code>:<\/p>\n\n\n\n<pre id=\"block-ab66be2e-d644-4cb4-aad7-32e74fd29d3d\" class=\"wp-block-code\"><code>aSavingsAccount: <strong>obj<\/strong> SavingsAccount(JonhSmith)\nnewBalance: <strong>var<\/strong> float\nnewBalance := aSavingsAccount.withdraw(340)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-e88485ca-1a7f-42ec-b341-411813ebf180\">Execution of <code>aSavingsAccount.withdraw(340)<\/code> takes place as follows:<\/p>\n\n\n\n<ol id=\"block-eaabbed4-0bde-4b3c-aee4-2649e62fc3bb\" class=\"wp-block-list\">\n<li>Execution starts by execution of the statements in <code>withdraw<\/code> as defined in <code>Account<\/code>.<\/li>\n\n\n\n<li>The first statement to execute is <code>inner<\/code>.<\/li>\n\n\n\n<li>Execution of <code>inner<\/code> implies that the statements of withdraw in <code>SavingsAccount<\/code> are executed.<\/li>\n\n\n\n<li>When the statements in <code>SavingsAccount<\/code> have been executed, control returns to the statements after <code>inner<\/code> in <code>Account<\/code>.<\/li>\n\n\n\n<li>This implies that <code>newB := balance<\/code> is executed<\/li>\n\n\n\n<li>Finally execution of <code>withdraw<\/code> is completed.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The figure below illustrates the execution of <code>aSavingsAccount.withdraw(340)<\/code>. The numbers show the order of execution of the involved statements:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"484\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/03\/SavingsAccountwithdraw340-1-1024x484.jpg\" alt=\"\" class=\"wp-image-4520\" style=\"width:834px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/03\/SavingsAccountwithdraw340-1-1024x484.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/03\/SavingsAccountwithdraw340-1-300x142.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/03\/SavingsAccountwithdraw340-1-768x363.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/03\/SavingsAccountwithdraw340-1.jpg 1318w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Sequence of actions for the call SavingsAccount.withdraw(340)<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-32489cea-421e-4ebb-a46d-bc600f2aa2ff\">We may extend <code>withdraw<\/code> in a similar way in <code>CreditAccount<\/code>:<\/p>\n\n\n\n<pre id=\"block-6b871289-92d2-4b12-bde1-25b9a2dd6d57\" class=\"wp-block-code\"><code><strong>class<\/strong> CreditAccount: Account\n   ...\n   withdraw ::\n      if (-balance &lt; maxCredit) :then\n         balance := balance - amount\n      :else \n         console.print(\"Not possible to withdraw beyond max credit\") <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-a92a2bf4-f50a-4136-8182-c14e11827774\">Invocation of <code>withdraw<\/code> on a <code>CreditAccount<\/code> object implies that execution of <code>inner<\/code> will execute the statements in <code>withdraw<\/code> in <code>SavingsAccount<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A virtual print method<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We now add one more virtual method to <code>Account<\/code> and its subclasses in the form of a print method that prints the status of a given account on the console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Part of the print of status is the same for all accounts whereas other parts are special for the subclasses.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):\n   ...\n   createStatement:&lt;\n      stmt: <strong>var<\/strong> String\n      stmt := \"Account statement for \" + owner.name + '\\n'\n              + \"The account is a \"\n      inner(Account)\n      stmt := stmt + \"The balance is: \" + balance + '\\n'\n   print:\n      console.print(createStatement)\n   sendEmail:\n      owner.email(createStatement)\n\n<strong>class<\/strong> SavingsAccount: Account\n   ...\n   createStatement::\n       stmt := stmt + \"savings account.\" + \n               \"\\nRelease date is \" + releaseDate.asText<\/code><\/pre>\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=1043\" 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>In this section, we will introduce virtual methods. Virtual methods may be used to specify partial methods that may be further extended in subclasses. In the next example, we define withdraw of Account as a virtual method: The symbol &#8216;:&lt;&#8216; specifies that withdraw is a virtual method. The statement part of withdraw consists of inner [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1008,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1043","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\/1043","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=1043"}],"version-history":[{"count":37,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1043\/revisions"}],"predecessor-version":[{"id":10435,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1043\/revisions\/10435"}],"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=1043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}