{"id":1606,"date":"2023-11-21T13:15:09","date_gmt":"2023-11-21T12:15:09","guid":{"rendered":"http:\/\/oopm.org\/?page_id=1606"},"modified":"2024-12-13T13:46:38","modified_gmt":"2024-12-13T12:46:38","slug":"7-1-extension-of-travel-booking-system","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=1606","title":{"rendered":"11.1 Self-constrained\u00a0virtual class"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1606&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=wpv2pages1606&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\">As a first example of a virtual class attribute, we extend the travel booking system introduced in section <script>mkRef(\"A travel booking system\");<\/script>. We will encapsulate details about a travel booking in a local nested virtual class <code>TravelInfo<\/code> as shown in the example below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>class<\/strong> TravelBooking: Booking\n      <strong>class<\/strong> TravelInfo:&lt;\n         source, destination: <strong>ref<\/strong> City\n         asString -&gt; s: <strong>var<\/strong> String:&lt;\n            inner(asString)\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>theCarrier: <strong>ref<\/strong> Carrier\n      theTravel: <strong>obj<\/strong> TravelInfo    \n      confirm::&lt; \n         confirmText := confirmText + theTravel.asString\n      cancel:\n         theTravel.theCarrier.notifyCancellation(bookingRef)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The symbol&nbsp;<code>':&lt;'<\/code>&nbsp;specifies that class <code>TravelInfo<\/code> is virtual in the same way as for virtual methods. <code>TravelInfo<\/code> has the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>source<\/code> and <code>destination<\/code> of the travel.<\/li>\n\n\n\n<li>A method <code>asString<\/code>, that returns a text about travel details that may be emailed to the customer.<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The virtual class <code>TravelInfo<\/code> is an example of a <em>self-constrained <\/em>virtual class, which is a virtual class that is constrained by an object descriptor specifying the virtual class itself, and this object-descriptor includes declarations and\/or statements.  This is in contrast to a class-constrained virtual class, which is constrained by a class name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The specification of <code>TravelInfo<\/code> may be extended in subclasses of <code>TravelBooking<\/code>, just like virtual methods may be extended. <code>TravelInfo<\/code> can only be extensions of the object descriptor specifying the virtual class. A class-constrained virtual class can be extended to any subclass of the virtual class constraining the virtual class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below, the specification of is extended in a way which is similar to the way a virtual method may be extended:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>class<\/strong> FlightBooking: TravelBooking \n      <strong>class<\/strong> TravelInfo::&lt;\n         fromAirport, toAirport: <strong>ref<\/strong> Airport\n         row: <strong>var<\/strong> integer  \n         seat: <strong>var<\/strong> char\n         asString::&lt;\n            s := theCarrier.asString + \n                 \" from \" + fromAirport.name + \n                 \" to \" + toAirport.name\n            inner(asString)        <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The extension (also called further binding) of <code>TravelInfo<\/code> works as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The symbol <code>'::&lt;' <\/code>specify that this declaration of <code>TravelInfo<\/code> extends (&#8216;<code>::'<\/code>) the one from the superclass <code>TravelBooking<\/code>, and that it is still virtual (<code>'&lt;'<\/code>).<\/li>\n\n\n\n<li>The extension includes reference variables <code>fromAirport<\/code> and <code>toAirport<\/code>.<\/li>\n\n\n\n<li>It includes a <code>row<\/code> and <code>seat<\/code> for this booking.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"766\" height=\"637\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-1.jpg\" alt=\"\" class=\"wp-image-9569\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-1.jpg 766w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-1-300x249.jpg 300w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><figcaption class=\"wp-element-caption\">Extending TravelInfo in subclass of TravelBooking<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The further binding\/extension of <code>TravelInfo<\/code> in <code>FlightBooking<\/code> implies that the complete definition of <code>TravelInfo<\/code> in <code>FlightBooking<\/code> is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"773\" height=\"285\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/TravelInfoComplete-1.jpg\" alt=\"\" class=\"wp-image-7372\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/TravelInfoComplete-1.jpg 773w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/TravelInfoComplete-1-300x111.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/TravelInfoComplete-1-768x283.jpg 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Also, note, that we in <code>FlightBooking<\/code> do <em>not<\/em> need to extend the virtual method <code>confirm<\/code>, since the information needed for <code>confirm<\/code> is computed by <code>asString<\/code> in <code>TravelInfo<\/code> in <code>TravelBooking<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may make a similar extension of <code>TravelInfo<\/code> in <code>TrainBooking<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>class<\/strong> TrainBooking: TravelBooking\n      <strong>class<\/strong> TravelInfo::&lt;\n         fromStation, toStation: <strong>ref<\/strong> TrainStation\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>waggon, seat: <strong>var<\/strong> integer\n         asString::&lt;\n            s := theCarrier.asString + \n            \" from: \" + fromStation.name + \" to: \" + toStation.name\n            inner(asString)   <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"527\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-2-1024x527.jpg\" alt=\"\" class=\"wp-image-9572\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-2-1024x527.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-2-300x154.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-2-768x395.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/ExtendingTravelInfo-2.jpg 1228w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Extending TravelInfo in two subclasses of TravelBooking<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the above example, we use the class <code>Carrier<\/code>, which is only sketched below. We may make subclasses of <code>Carrier<\/code> representing airlines and railways as sketches below. The reference <code>theCarrier<\/code> in <code>FlightBooking<\/code> may then refer to an <code>Airline<\/code> object and in <code>TrainBooking<\/code> to a <code>Railway<\/code> object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Carrier:\n   notifyCancellation(bookingRef: <strong>var<\/strong> String):\n      ...\n<strong>class<\/strong> Airline: Carrier\n   ...\n<strong>class<\/strong> RailWay: Carrier\n   ...<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"769\" height=\"471\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/BookingAndTravelinfoHierarchyAndNesting.jpg\" alt=\"\" class=\"wp-image-8555\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/BookingAndTravelinfoHierarchyAndNesting.jpg 769w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/BookingAndTravelinfoHierarchyAndNesting-300x184.jpg 300w\" sizes=\"(max-width: 769px) 100vw, 769px\" \/><figcaption class=\"wp-element-caption\">BookingAndTravelinfoHierarchyAndNesting\/<\/figcaption><\/figure>\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=1606\" 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>As a first example of a virtual class attribute, we extend the travel booking system introduced in section . We will encapsulate details about a travel booking in a local nested virtual class TravelInfo as shown in the example below: The symbol&nbsp;&#8216;:&lt;&#8216;&nbsp;specifies that class TravelInfo is virtual in the same way as for virtual methods. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1410,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1606","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\/1606","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=1606"}],"version-history":[{"count":65,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1606\/revisions"}],"predecessor-version":[{"id":10345,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1606\/revisions\/10345"}],"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=1606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}