{"id":2805,"date":"2023-07-16T08:30:00","date_gmt":"2023-07-16T06:30:00","guid":{"rendered":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2805"},"modified":"2023-07-18T10:15:52","modified_gmt":"2023-07-18T08:15:52","slug":"liskov-substitution-principle","status":"publish","type":"post","link":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2805","title":{"rendered":"Liskov Substitution Principle"},"content":{"rendered":"<p style=\"text-align: justify;\">Third SOLID principle is relied to second principle spoken in previous article. This principle concerns to correctly projected earlier inheritance &#8211; if derived class is created, there is an urgent need to use derived class instead of base class. Otherwise it means, that inheritance has been wrongly implemented. Simply saying: compatibility of interface and every single method has to be correct in every aspect. Code below shows wrongly implemented inheritance:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nabstract class Animal\n{\n    public abstract void Fly();\n    public string Name { get; set; }\n   \n}\n\nclass Bird: Animal\n{\n    public override void Fly()\n    {\n        Console.WriteLine(&quot;Bird flies&quot;);\n    }\n}\n\nclass Fish : Animal\n{\n    public override void Fly()\n    {\n        throw new NotImplementedException(&quot;Fish can't fly!&quot;); \n    }\n}\n\n\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">We have created abstract class called <span style=\"background-color: #efefef;\">Animal<\/span>, the most perceptive ones have noticed, that the inheritance mechanism has been wrongly considered. Fish is an animal, but it can&#8217;t fly right? And that is our Liskov&#8217;s &#8222;broken rule&#8221;\u00a0 \u00a0substitution. The inheritance has to be considered in different way, so the every derived class can use every aspect of base class. In good planned inheritance mechanism, derived class shouldn&#8217;t overwrite base classes. They can alternatively expand them by evoking them from base class. Let us consider properly considered example:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nclass BreweryMachine\n{\n    public virtual void Brew()\n    {\n        Console.WriteLine(&quot;Pour some water to the tub&quot;);\n        Console.WriteLine(&quot;Insert hop to the tub&quot;);\n    }\n}\n\nclass AleBeer: BreweryMachine\n{\n    public override void Brew()\n    {\n        base.Brew();\n        Console.WriteLine(&quot;Brew beer&quot;);\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        BreweryMachine beer;\n        \n        Console.WriteLine(&quot;Making  an ordinary beer&quot;);\n        bee r= new BreweryMachine();\n        beer.Brew();\n        \n        Console.WriteLine(&quot;Making ale beer&quot;);\n        beer= new AleBeer();\n        beer.Brew();\n    }\n}\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">Example shown above precisely restricts Liskov Substitiution principle. You can not only use now object of derived class in base class, but in addition despite using polymorphism, we don&#8217;t overwrite methods of base class.<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8222;If it looks like a duck, quacks like a duck, but needs batteries &#8211; you probably have the wrong abstraction&#8221;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[1,13],"tags":[],"class_list":["post-2805","post","type-post","status-publish","format-standard","hentry","category-bez-kategorii","category-solid-articles"],"_links":{"self":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2805"}],"version-history":[{"count":13,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2805\/revisions"}],"predecessor-version":[{"id":2822,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2805\/revisions\/2822"}],"wp:attachment":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}