{"id":9811,"date":"2024-11-26T11:32:58","date_gmt":"2024-11-26T10:32:58","guid":{"rendered":"https:\/\/oopm.org\/?page_id=9811"},"modified":"2024-12-13T11:29:41","modified_gmt":"2024-12-13T10:29:41","slug":"11x-summary-of-virtuals","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=9811","title":{"rendered":"12 Summary of virtual classes and virtual methods"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages9811&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=wpv2pages9811&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 this chapter, we summarize virtual classes and virtual methods, including using virtuals as parameters. The purpose of this chapter is to have a complete description of virtual classes and virtual methods at one place in the book. To learn about these mechanism, the reader should consult the section on <script>mkRef(\"Virtual methods\")<\/script> and the chapter on <script>mkRef(\"Virtual classes\")<\/script>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Virtual classes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A virtual class declaration has one of the following forms:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Self-constrained virtual class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> <em>ClassName<\/em>(<em>Parameters<\/em>) <em>ReturnValue<\/em>:&lt; <em>SuperClass<\/em>\n   <em>Declarations<\/em>\n   <em>Statements<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The only difference from the declaration of a non-virtual class (see <script>mkRef(\"Class details\")<\/script>) is the use of <code>':&lt;'<\/code> instead of just <code>':'<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A declaration of this form specifies a <em>self-constrained virtual class<\/em> attribute (<script>mkRef(\"Self-constrained\u00a0virtual class\")<\/script>) in the enclosing method or class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Class-constrained virtual class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> <em>ClassName<\/em>(<em>Parameters<\/em>) <em>ReturnValue<\/em>:&lt; <em>SuperClass<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A declaration of this form specifies a <em>class-constrained virtual class<\/em> attribute (<script>mkRef(\"Class-constrained virtual class\")<\/script>) in the enclosing method or class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Further binding<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A further binding of a virtual class has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> <em>ClassName<\/em>::&lt; <em>SuperClass<\/em>\n   <em>Declarations<\/em>\n   <em>Statements<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The symbol &#8216;<code>::&lt;'<\/code> specifies that this is a <em>further binding<\/em>. The class is still virtual and may be further bound\/extended in subclasses\/submethods of the enclosing class\/method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above form is the general form for a further binding of a virtual class. For a self-constrained virtual class, no&nbsp;<code>SuperClass<\/code>&nbsp;can be specified since the virtual binding in the enclosing superclass is the implicit superclass. Consider the example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> GhostA:\n   <strong>class<\/strong> C:&lt;\n      ...\n<strong>class<\/strong> GhostB: GhostA\n   <strong>class<\/strong> C::&lt; \n<strong>      <\/strong>...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example shows the general form of a further binding of a self-constrained virtual method. The virtual class&nbsp;<code>C<\/code>&nbsp;defined in&nbsp;<code>GhostA<\/code>&nbsp;is the implicit superclass of the further binding of&nbsp;<code>C<\/code>&nbsp;in&nbsp;<code>GhostB<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A further binding of a class-defined virtual class may look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> GhostX:\n   <strong>class<\/strong> C:&lt; T\n<strong>class<\/strong> GhostY: GhostA\n   <strong>class<\/strong> C::&lt; TT<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class-defined virtual&nbsp;<code>C<\/code>&nbsp;is constrained by the class&nbsp;<code>C<\/code>. In&nbsp;<code>GhostY<\/code>, a further binding of&nbsp;<code>C<\/code>&nbsp;is is made in the form of the class&nbsp;<code>TT<\/code>, which must be a subclass of&nbsp;<code>T<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to add declaratios and\/or statements to a further binding as indicated here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> GhostX:\n   <strong>class<\/strong> C:&lt; T\n<strong>class<\/strong> GhostY: GhostA\n   <strong>class<\/strong> C::&lt; TT\n      Dcl1\n      Dcl2\n      ...\n      Stm1\n      Stm2\n      ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If declarations and\/or statements are added in the further binding, the virtual class then<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>becomes a self-constrained virtual class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Final binding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> <em>ClassName<\/em>:: <em>SuperClass<\/em>\n   <em>Declarations<\/em>\n   <em>Statements<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The symbol &#8216;<code>::'<\/code> specifies that this is a <em>final binding.<\/em> The class is no longer virtual, which means that it is not possible to make further bindings in subclasses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As for further binding, the above form is the general form of a final binding. With respect to <em><code>SuperClass<\/code><\/em>, the same rules apply here as for a further binding as described above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Virtual methods<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The declaration and bindings of a virtual method is analogous to the declaration and bindings of a virtual class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A virtual method declaration has one of the following form:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Self-constrained virtual method<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><em>MethodName<\/em>(<em>Parameters<\/em>) <em>ReturnValue<\/em>:&lt; <em>SuperMethod<\/em>\n   <em>Declarations<\/em>\n   <em>Statements<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The only difference from the declaration of a non-virtual method (see <script>mkRef(\"Method details\")<\/script>) is the use of <code>':&lt;'<\/code> instead of just <code>':'<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A declaration of this form specifies a <em>self-constrained virtual method attribute<\/em> in the enclosing method or class. Note, we have not used the term <em>self-constrained virtual method<\/em> before.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method-constrained virtual method<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><em>MethodName<\/em>(<em>Parameters<\/em>) <em>ReturnValue<\/em>:&lt; <em>SuperMethod<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A declaration of this form specifies a <em>method-constrained virtual method attribute<\/em> in the enclosing method or class. Note, we have no examples of method-constrained virtual methods in this version of the book. There are included here for completeness and to illustrate that virtual methods and virtual classes have analogous forms with respect to syntax and semantics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Further binding<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A further binding of a virtual method has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>MethodName<\/em>::&lt; <em>SuperMethod<\/em>\n   <em>Declarations<\/em>\n   <em>Statements<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The symbol &#8216;<code>::&lt;'<\/code> specifies that this is a <em>further binding<\/em>. The method is still virtual and may be further bound\/extended in subclasses\/submethods off the enclosing class\/method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above form is the general form for a further binding of a virtual method. For a self-constrained virtual method as used in this book, no <code>SuperMethod<\/code> can be specified since the virtual binding in the enclosing superclass is the implicit supermethod. Consider the example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> GhostA:\n   M:&lt;\n      ...\n<strong>class<\/strong> GhostB: GhostA\n   M::&lt; \n      ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example shows the general form of a further binding of a self-constrained virtual method. The virtual method <code>M<\/code> defined in <code>GhostA<\/code> is the implicit supermethod of the further binding of <code>M<\/code> in <code>GhostB<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For completeness, we also sketch how the binding of a method-defined virtual method may look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> GhostX:\n   M:&lt; A\n<strong>class<\/strong> GhostY: GhostA\n   M::&lt; AA<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method-defined virtual <code>M<\/code> is constrained by the method <code>A<\/code>. In <code>GhostY<\/code>, a further binding of <code>M<\/code> is is made in the form of the method <code>AA<\/code>, which must be a submethod of <code>A<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to add declarations and\/or statements to a further binding. The virtual method then becomes a self-constrained virtual method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Final binding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><em>MethodName<\/em>:: <em>SuperMethod<\/em>\n   <em>Declarations<\/em>\n   <em>Statements<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The symbol &#8216;<code>::'<\/code> specifies that this is a <em>final binding.<\/em> The method is no longer virtual, which means that it is not possible to make further bindings in subclasses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As for further binding, the above form is the general form of a final binding. With respect to <em>SuperMethod<\/em>, the same rules apply here as for a further binding as described above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Virtuals as parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A parameter of a method or class may be declared as a class-constrained virtual class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In chapter <script>mkRef(\"Collections\")<\/script>, the parameters <code>ElmType<\/code> of class <code>Set<\/code>, class <code>Array<\/code> and class <code>OrderedList<\/code> are examples of class-constrained virtual class parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The invocation of the method or instantiation of the class may then specify a final binding of the virtual class parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a similar way, a parameter of a method or class may be declared as a method-constrained virtual method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In section <script>mkRef(\"Conditional statements\")<\/script>, the parameters <code>thenPart<\/code> and <code>elsePart<\/code> of <code>if:then:else<\/code> are examples of method-constrained virtual method parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The invocation of the method or instantiation of the class may then specify a final binding of the virtual method parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/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=9811\" 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 chapter, we summarize virtual classes and virtual methods, including using virtuals as parameters. The purpose of this chapter is to have a complete description of virtual classes and virtual methods at one place in the book. To learn about these mechanism, the reader should consult the section on and the chapter on . [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-9811","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\/9811","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=9811"}],"version-history":[{"count":40,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/9811\/revisions"}],"predecessor-version":[{"id":10321,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/9811\/revisions\/10321"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}