{"id":1618,"date":"2023-11-21T14:00:50","date_gmt":"2023-11-21T13:00:50","guid":{"rendered":"http:\/\/oopm.org\/?page_id=1618"},"modified":"2024-12-13T13:47:17","modified_gmt":"2024-12-13T12:47:17","slug":"7-2-general-qualified-virtual-class","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=1618","title":{"rendered":"11.2 Class-constrained virtual class"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1618&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=wpv2pages1618&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\">In chapter <script>mkRef(\"Collections\");<\/script>, we introduced&nbsp;class&nbsp;<code>Set<\/code>, which has a class&nbsp;<code>ElmType<\/code>&nbsp;as a parameter. Class&nbsp;<code>ElmType<\/code>&nbsp;is an example of a <em>class-constrained<\/em> virtual class. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We first show how to define class <code>ElmType<\/code> as a local attribute and not a parameter to illustrate the similarity with respect to virtual class <code>TravelInfo<\/code> defined in the previous section. To be able to distinguish from the <code>Set<\/code> defined in chapter <script>mkRef(\"Collections\");<\/script>, the class below is called <code>ObjectSet<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> ObjectSet:\n   <strong>class<\/strong> ElmType:&lt; Object\n   insert(E: <strong>ref<\/strong> ElmType): ...\n   has(E: <strong>ref<\/strong> ElmType): ...\n   remove(E: <strong>ref<\/strong> ElmType): ...\n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The declaration of class <code>ElmType<\/code> here specifies a local class attribute like class <code>TravelInfo<\/code> in the previous section. The difference is that class <code>ElmType<\/code> is constrained by a class, in this case the pre-defined class <code>Object<\/code>. The constraint of class <code>ElmType<\/code> specifies that it must be a subclass of <code>Object<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may extend the specification of <code>ElmType<\/code> in subclasses of <code>ObjectSet<\/code>. Here we define a class <code>AccountSet<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> AccountSet: ObjectSet\n   <strong>class<\/strong> ElmType ::&lt; Account  <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With this binding\/extension of <code>ElmType<\/code> to <code>Account<\/code>, instances of <code>AccountSet<\/code> may only contain objects of type <code>Account<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>as: <strong>obj<\/strong> AccountSet\nr: <strong>obj<\/strong> Customer\na: <strong>obj<\/strong> Account\nas.insert(r)  -- illegal\nas.insert(a)  -- legal<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We may further extend <code>ElmType<\/code> in subclasses of <code>AccountSet<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> SavingsAccountSet: AccountSet\n   <strong>class<\/strong> ElmType::&lt; SavingsAccount<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Instances of <code>SavingsAccountSet<\/code> may only contain objects of type <code>SavingsAccount<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Virtual classes as parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, class <code>Set<\/code> as introduced in chapter <script>mkRef(\"Collections\");<\/script> has class <code>ElmType<\/code> as a parameter. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Set(<strong>class<\/strong> ElmType:&lt; Object):  \n   insert(e: <strong>ref<\/strong> ElmType): ...\n   has(e: <strong>ref<\/strong> ElmType): ...\n   remove(e: <strong>ref<\/strong> ElmType): ...\n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A parameter is in general similar to a local attribute &#8211; the difference being that a value (an actual parameter) for the parameter must be supplied when the class (or method) is instantiated. You may also supply the actual parameter when a class or method is used as a superclass or supermethod respectively.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Declaring class <code>ElmType<\/code> as a parameter thus works in the same way as for the <code>ObjectSet<\/code> with a local virtual class <code>ElmType<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In chapter <script>mkRef(\"Collections\");<\/script>, we showed how to create an instance of class <code>Set<\/code> and supplying an actual parameter for <code>element<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>theAccountsFile: <strong>obj<\/strong> Set(Account)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the specification of the virtual class is extended by supplying an actual parameter for <code>ElmType<\/code>. This is actually a binding of <code>ElmType<\/code> to <code>Account<\/code>. We may use class <code>ObjectSet<\/code> to make a similar object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>theAccountsFileX: <strong>obj<\/strong> ObjectSet\n   <strong>class <\/strong>ElmType::&lt; Account \n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here the binding of <code>ElmType<\/code> to <code>Account<\/code> also<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>extends the description of <code>ElmType<\/code> in a sub-descriptor of class <code>ObjectSet<\/code>.<\/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=1618\" 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 chapter , we introduced&nbsp;class&nbsp;Set, which has a class&nbsp;ElmType&nbsp;as a parameter. Class&nbsp;ElmType&nbsp;is an example of a class-constrained virtual class. We first show how to define class ElmType as a local attribute and not a parameter to illustrate the similarity with respect to virtual class TravelInfo defined in the previous section. To be able to distinguish [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1410,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1618","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\/1618","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=1618"}],"version-history":[{"count":64,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1618\/revisions"}],"predecessor-version":[{"id":10346,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1618\/revisions\/10346"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1410"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}