{"id":442,"date":"2019-11-27T11:32:00","date_gmt":"2019-11-27T18:32:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=442"},"modified":"2019-11-25T22:28:57","modified_gmt":"2019-11-26T05:28:57","slug":"overriding","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/11\/27\/overriding\/","title":{"rendered":"Overriding"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes when you create a subclass you want to your subclass to do something different than the class it inherits from. But you want to retain the API or reuse the method name because its perfectly suited.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you do this and add a method that has the EXACT SAME signature as the one in the superclass you are <em>overriding<\/em> the method. The signature includes the method name, parameters and their types but excludes the return type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can be very useful if you have a class that has many specialized  subclasses. For instance, the framework has several subclasses of TCP Sockets; HTTPSockets, SMTPSockets and POP3Sockets. What an HTTP Socket does when you call it Connect method may be different than what the SMTP Socket does when you call its Connect method and also different from what the POP3 Socket does when you call its connect method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you have classes like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class BaseClass\n\tSub Foo()\n\t  System.debuglog CurrentMethodName\n\t  \n\tEnd Sub\nEnd Class\n\nClass SubClassA\nInherits BaseClass\n\tSub Foo()\n\t  System.debuglog CurrentMethodName\n\t\t  \n\tEnd Sub\nEnd Class\n\nClass SubclassB\nInherits BaseClass\n\tSub Foo()\n\t  System.debuglog CurrentMethodName\n\t\t  \n\tEnd Sub\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And code that uses these classes as <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nDim c As BaseClass\n\nc = New SubClassA\n\nc.foo\n\nc = New SubclassB\n\nc.foo\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">What you will see in the debuglog is something like this on macOS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10:09:20 PM : My Application Launched\n              SubClassA.Foo\n              SubclassB.Foo\n10:09:27 PM : My Application Ended<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Whats going on ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Methods are, unlike properties, virtual. What that means is that the correct method to call will be determined NOT at compile time, but at runtime and based on the actual instance and its method list. This is really handy in lots of ways as I already noted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can, as we did in our code, declare the type as the most generic &#8211; like we did &#8211; since all subclasses are instance of the subclass AND whatever the superclasses that its inherited.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In our code above an instance of SubClassA is both a SubclassA AND a BaseClass. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are some glitches that you can still run into. For instance <a href=\"http:\/\/feedback.xojo.com\/case\/57732\">this bug report <\/a> notes one spot where the correct method to call is NOT based on the runtime type but the compile time type (which is really counter intuitive)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That said, overriding is very useful in a class hierarchy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Have fun with this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes when you create a subclass you want to your subclass to do something different than the class it inherits from. But you want to retain the API or reuse the method name because its perfectly suited. When you do this and add a method that has the EXACT SAME signature as the one in &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/11\/27\/overriding\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Overriding&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-442","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions\/449"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}