{"id":99,"date":"2016-10-28T13:24:40","date_gmt":"2016-10-28T18:24:40","guid":{"rendered":"https:\/\/chibicode.org\/?p=99"},"modified":"2016-10-28T13:24:40","modified_gmt":"2016-10-28T18:24:40","slug":"protocols-default-implementations-and-class-inheritance","status":"publish","type":"post","link":"https:\/\/chibicode.org\/?p=99","title":{"rendered":"Protocols, Default Implementations and Class Inheritance"},"content":{"rendered":"<p>Say you have the following setup:<\/p>\n<ul>\n<li>A protocol named <code>Doable<\/code> that defines the <code>doSomething()<\/code> method.<\/li>\n<li>A default implementation for the <code>doSomething()<\/code> method in a protocol extension.<\/li>\n<li>A base class that conforms to <code>Doable<\/code>, but does not implement the <code>doSomething()<\/code> method itself.<\/li>\n<li>A sub-class inheriting from the base class which provides a custom implementation of the <code>doSomething()<\/code> method.<\/li>\n<li>An array of mixed base and subclass instances that is type <code>[Doable]<\/code>.<\/li>\n<\/ul>\n<p>The results of invoking <code>doSomething()<\/code> on all elements of the array may surprise you. When the for loop \/ reduce \/ whatever invokes <code>doSomething()<\/code> on a member of the array which is a subclass, you will <strong>not<\/strong> get the subclass&#8217; custom implementation. Instead, you will get the default implementation!<\/p>\n<p>When the runtime goes looking for <code>doSomething()<\/code> on the current object (of type <code>Doable<\/code>) in the loop, it looks to the object which actually conforms to the <code>Doable<\/code> protocol, which is the base class. The runtime checks to see if the class implements the method, and when it sees that the base class does not, it falls back to the default implementation, rather than seeing if the subclass implements it. Apparently, the subclass is only checked in instances where it is <em>overriding<\/em> a method explicitly defined on its superclass.<\/p>\n<p>So, the solution is actually quite simple:<\/p>\n<ul>\n<li>Provide an implementation of <code>doSomething()<\/code> on the base class. It can just be a duplicate of the default implementation, if that&#8217;s the behavior you want for it.<\/li>\n<li>Change the subclass&#8217; <code>doSomething()<\/code> implementation to include an <code>override<\/code> declaration.<\/li>\n<\/ul>\n<p>That&#8217;s it! The next time you run your loop, the sub-class will have it&#8217;s doSomething() method called. I made a <a href=\"https:\/\/gist.github.com\/JoshuaSullivan\/0752d008e1aa08febdd18c25954183d7\" target=\"_blank\">playground<\/a> for you to check this out (turn on documentation rendering):<\/p>\n<p>[gist https:\/\/gist.github.com\/JoshuaSullivan\/0752d008e1aa08febdd18c25954183d7]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say you have the following setup: A protocol named Doable that defines the doSomething() method. A default implementation for the doSomething() method in a protocol extension. A base class that conforms to Doable, but does not implement the doSomething() method itself. A sub-class inheriting from the base class which provides a custom implementation of the &hellip; <a href=\"https:\/\/chibicode.org\/?p=99\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Protocols, Default Implementations and Class Inheritance&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":4,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":103,"href":"https:\/\/chibicode.org\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/103"}],"wp:attachment":[{"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chibicode.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}