{"id":7831,"date":"2024-09-30T10:03:39","date_gmt":"2024-09-30T08:03:39","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7831"},"modified":"2025-01-16T08:49:54","modified_gmt":"2025-01-16T07:49:54","slug":"8-5-benefits-of-subclasses-and-submethods","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7831","title":{"rendered":"8.5 Rationale for subclasses and submethods"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7831&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=wpv2pages7831&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\">The <em>main rationale<\/em> for subclasses and submethods is to be able to represent <em>classification hierarchie<\/em>s. In the next section, we describe classification hierarchies, but we have already seen examples of such hierarchies. In section <script>mkRef(\"Data items\")<\/script>, the figure showing the relationships between <code>Datum<\/code>, <code>Reference<\/code>, <code>Value<\/code> and &#8216;specializations&#8217; of <code>Value<\/code> is one example, and the figure showing <code>Date<\/code> and <code>TimeOfDay<\/code> as special <code>Value<\/code> types is another example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is also a <em>secondary rationale<\/em> that is of a <em>technical<\/em> nature in the sense of how subclass and submethods works. We elaborate on this technical rationale in the rest of this section, but you should keep in mind that the main rationale is the represent classification hierarchies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The subclass mechanism ensures that an instance of a subclass has all the attributes defined in its superclass. This means that a reference to an instance of a subclass can be assigned to a reference variable typed with a superclass and the common attributes in the superclass may then be accessed through this reference variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The subclass mechanism makes it possible to write code that can handle objects of a givne class and objects being instances of subclasses of such a class. This is often referred to as the principle of <em>substitutability<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned in section mkRef(&#8220;Extended type rule&#8221;), we may write code that works for all <code>Account<\/code> objects including instances of subclasses of <code>Account<\/code>. The  code is safe in the sense that that the compiler can verify that all attributes of an object being accessed are in fact defined for this object.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is, however, an issue with code executed in subclasses and methods&nbsp;(virtual and non-virtual) defined in subclasses. For a given class, execution of a method on an instance of the class, has a specific effect on the object. If the method or class is virtual and extended in a subclass, the new code may \u2018spoil\u2019 the effect and this may be problematic.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As an example, the method&nbsp;<code>deposit<\/code>&nbsp;of class&nbsp;<code>Account<\/code>&nbsp;increments the balance of the&nbsp;<code>Account<\/code>-object. If&nbsp;<code>deposit<\/code>&nbsp;is virtual, we may extend the definiton of&nbsp;<code>deposit<\/code>&nbsp;in a subclass like&nbsp;<code>SavingsAccount<\/code>&nbsp;and here we may set&nbsp;<code>balance<\/code>&nbsp;to any value, which implies that&nbsp;<code>balance<\/code>&nbsp;may not have been increased. In general, this kind of code should be avoided. We want our subclasses to be&nbsp;<em>behavioural compatible<\/em>&nbsp;with their superclass. The compiler\/language cannot guarantee that this is the case \u2013 it is up to the programmer to ensure this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the advantages of the class\/subclass mechanism, is that it supports&nbsp;<em>reuse of code<\/em>. For the bank system, the common code for&nbsp;<code>SavingsAccount<\/code>&nbsp;and&nbsp;<code>CreditAccount<\/code>&nbsp;is placed in class&nbsp;<code>Account<\/code>&nbsp;and needs thus not to be repeated in&nbsp;<code>SavingsAccount<\/code>&nbsp;and&nbsp;<code>CreditAccount<\/code>. This is in practice a major benefit of the class\/subclass mechanism.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In most of the literature of object-oriented programming reuse is considered the main advantage of the class\/subclass mechanism. As said, we think the main advantage is the ability to represent classification hierarchies \u2013 reuse is an important but secondary sidebenefit. In the next section, we explain what we understand as a classification hierarchy.<\/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=7831\" 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>The main rationale for subclasses and submethods is to be able to represent classification hierarchies. In the next section, we describe classification hierarchies, but we have already seen examples of such hierarchies. In section , the figure showing the relationships between Datum, Reference, Value and &#8216;specializations&#8217; of Value is one example, and the figure showing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1008,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7831","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\/7831","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=7831"}],"version-history":[{"count":20,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7831\/revisions"}],"predecessor-version":[{"id":11283,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7831\/revisions\/11283"}],"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=7831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}