{"id":300,"date":"2022-11-30T23:33:08","date_gmt":"2022-11-30T23:33:08","guid":{"rendered":"https:\/\/codingwithleo.xyz\/?p=300"},"modified":"2024-04-29T17:30:47","modified_gmt":"2024-04-29T17:30:47","slug":"solid-the-liskov-substitution-principle","status":"publish","type":"post","link":"https:\/\/codingwithleo.xyz\/index.php\/2022\/11\/30\/solid-the-liskov-substitution-principle\/","title":{"rendered":"SO[L]ID \u2013 The Liskov substitution principle"},"content":{"rendered":"\n<p>The Liskov substitution principle is one of the most important principles of the SOLID principles because it describes how the object orientation must work. Basically, it says that an object of class B and another one of class C, both inherited from a class A, when instanced within a base class variable and some method is called, both the subclasses method should return\/work properly without breaking the program.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"396\" src=\"https:\/\/codingwithleo.xyz\/wp-content\/uploads\/2023\/03\/Untitled-Diagram2-1024x396.png\" alt=\"\" class=\"wp-image-301\" srcset=\"https:\/\/codingwithleo.xyz\/wp-content\/uploads\/2023\/03\/Untitled-Diagram2-1024x396.png 1024w, https:\/\/codingwithleo.xyz\/wp-content\/uploads\/2023\/03\/Untitled-Diagram2-300x116.png 300w, https:\/\/codingwithleo.xyz\/wp-content\/uploads\/2023\/03\/Untitled-Diagram2-768x297.png 768w, https:\/\/codingwithleo.xyz\/wp-content\/uploads\/2023\/03\/Untitled-Diagram2-1536x594.png 1536w, https:\/\/codingwithleo.xyz\/wp-content\/uploads\/2023\/03\/Untitled-Diagram2.png 1654w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As we can see above, all the classes &#8220;Fiat&#8221;, &#8220;Chevrolet&#8221;, &#8220;Hyundai&#8221; and &#8220;Tesla&#8221; inherits the class &#8220;Car&#8221;. The problem here is, despite it&#8217;s violating <strong><a href=\"https:\/\/codingwithleo.xyz\/index.php\/2022\/12\/03\/solid-the-interface-segregation-principle\/\" data-type=\"URL\" data-id=\"https:\/\/codingwithleo.xyz\/index.php\/2022\/12\/03\/solid-the-interface-segregation-principle\/\" target=\"_blank\" rel=\"noreferrer noopener\">The Interface Segregation Principle<\/a><\/strong>, the method called putGas will be useless for the class &#8220;Tesla&#8221; and its return will break the program depending on how it&#8217;ll be coded. Take a look at the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">class Car\n{\npublic:\n\n\tvirtual void accelerate()\n\t{ \/* Increment the velocity *\/ }\n\tvirtual void brake()\n\t{ \/* Reduce the velocity *\/ }\n\tvirtual void putGas()\n\t{ \/* Insert some fuel in the tank *\/ }\n\tvirtual ~Car();\n}\n\nclass Fiat : public Car\n{\npublic:\n\n\tFiat() = default;\n\t~Fiat();\n}\n\nclass Chevrolet : public Car\n{\npublic:\n\n\tChevrolet() = default;\n\t~Chevrolet();\n}\n\nclass Hyundai : public Car\n{\npublic:\n\n\tHyundai() = default;\n\t~Hyundai();\n}\n\nclass Tesla : public Car\n{\npublic:\n\tTesla() = default;\n\tvirtual void putGas() override \n\t{ throw \"There's no fuel tank on this car\" }\n\t~Tesla();\n}<\/code><\/pre>\n\n\n\n<p>As &#8220;Tesla&#8221; is an electric car, the method putGas() will throw an exception that will break the program execution if it is called. Let&#8217;s do some tests: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int main()\n{\n\tstd::vector&lt;Car&gt; Cars;\n\tCars.push_back(Fiat());\t\n\tCars.push_back(Chevrolet());\t\n\tCars.push_back(Hyundai());\t\n\tCars.push_back(Tesla());\t\n\t\n\t\/* Here we are accelerating all the cars *\/\n\tfor(Car&amp; _Car : Cars)\n\t{\n\t\t_Car.accelerate();\n\t}\n\t\n\t\/* Next, we will brake all the cars, this way: *\/\n\tfor(Car&amp; _Car : Cars)\n\t{\n\t\t_Car.brake();\n\t}\n\t\n\t\/* Now the gas is over, so, we will take some gas. *\/\n\tfor(Car&amp; _Car : Cars)\n\t{\n\t\t_Car.putGas();\n\t}\n\t\n\treturn 1;\n}<\/code><\/pre>\n\n\n\n<p>The line <code>_Car.putGas();<\/code> will run fine for the first 3 elements of the vector, in the last element which is of the class &#8220;Tesla&#8221; it will throw an exception and block the program.<\/p>\n\n\n\n<p>The Liskov substitution principle says: <strong>If you can invoke a method q() of a base class T, so you must be able to invoke a method q() of a subclass (derivated class) of T.<\/strong><\/p>\n\n\n\n<p>It&#8217;s a really basic design problem. When you look at our classes construction above, it&#8217;s obvious that something is wrong and we could solve this problem using  <strong><a href=\"https:\/\/codingwithleo.xyz\/index.php\/2022\/12\/03\/solid-the-interface-segregation-principle\/\" data-type=\"URL\" data-id=\"https:\/\/codingwithleo.xyz\/index.php\/2022\/12\/03\/solid-the-interface-segregation-principle\/\" target=\"_blank\" rel=\"noreferrer noopener\">The Interface Segregation Principle<\/a><\/strong> that you can find in the link.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Liskov substitution principle is one of the most important principles of the SOLID principles because it describes how the object orientation must work. Basically, it says that an object of class B and another one of class C, both inherited from a class A, when instanced within a base class variable and some method [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,6,7],"tags":[],"_links":{"self":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts\/300"}],"collection":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=300"}],"version-history":[{"count":5,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":340,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts\/300\/revisions\/340"}],"wp:attachment":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}