{"id":797,"date":"2022-02-21T20:58:12","date_gmt":"2022-02-21T19:58:12","guid":{"rendered":"https:\/\/wojciechsiwek.pl\/?p=797"},"modified":"2025-02-02T11:13:08","modified_gmt":"2025-02-02T10:13:08","slug":"zasada-podstawiania-liskov","status":"publish","type":"post","link":"https:\/\/wojciechsiwek.pl\/en\/zasada-podstawiania-liskov\/","title":{"rendered":"Liskov substitution principle"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Spis Tre\u015bci<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a4f53475ad57\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a4f53475ad57\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/wojciechsiwek.pl\/en\/zasada-podstawiania-liskov\/#Troche_teorii\" >Some theory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/wojciechsiwek.pl\/en\/zasada-podstawiania-liskov\/#Slynny_przyklad_z_kwadratem\" >The famous example with a square<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"troche-teorii\"><span class=\"ez-toc-section\" id=\"Troche_teorii\"><\/span>Some theory<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The third principle that I will discuss in the series of articles on SOLID will be the Liskov substitution principle. This principle is directly related to the inheritance mechanism and its content is as follows:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Functions that use pointers or references to base classes must be able to use class objects that inherit base classes as well, without knowing exactly those objects.<\/p><cite>Wikipedia<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">It looks scary, doesn't it? I think this principle can be presented a bit simpler and more intuitively:<\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>Where you are using a base class, you should be able to use a class that inherits from it<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"slynny-przyklad-z-kwadratem\"><span class=\"ez-toc-section\" id=\"Slynny_przyklad_z_kwadratem\"><\/span>The famous example with a square<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The simplest and best demonstration of this principle is the example with calculating the area of a rectangle and a square. We create a base class of a rectangle with a method that calculates the area of the figure and a square class that inherits from the rectangle (after all, a square is a rectangle):<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-java\" data-line=\"\">public class Rectangle {\n    protected int width;\n    protected int height;\n\n    public void setWidth(int width) {\n        this.width = width;\n    }\n\n    public void setHeight(int height) {\n        this.height = height;\n    }\n\n    public int calculateArea() {\n        return width * height;\n    }\n}<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-java\" data-line=\"\">public class Square extends Rectangle {\n    public void setWidth(int width) {\n        this.width = width;\n        this.height = width;\n    }\n\n    public void setHeight(int height) {\n        this.width = height;\n        this.height = height;\n    }\n}<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Now let's use our classes according to the code below and think about the results we will get:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-java\" data-line=\"\">public class Launcher {\n\n    public static void main(String[] args) {\n        \/\/make operations using base class\n        Rectangle rectangle = new Rectangle();\n        rectangle.setWidth(5);\n        rectangle.setHeight(10);\n        System.out.println(rectangle.calculateArea());\n\n        \/\/make operations using polymorphism\n        Rectangle rectangleSquare = new Square();\n        rectangleSquare.setWidth(5);\n        rectangleSquare.setHeight(10);\n        System.out.println(rectangleSquare.calculateArea());\n\n        \/\/make operations using inherited class\n        Square square = new Square();\n        square.setWidth(5);\n        square.setHeight(10);\n        System.out.println(square.calculateArea());\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you have already analyzed the above code, you will surely notice that something is wrong here. While from the first equation we get the correct value of 50, then from the next equation we get the value 100 and again 100.<\/p>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">This is an example of breaking the Liskov substitution rule - code execution has different effects depending on the reference to the base class or child class. So again: <strong> The child class should not change the behavior of the base class. <\/strong><\/p>","protected":false},"excerpt":{"rendered":"<p>The third article in the series about the SOLID principles. This time there is a Liskov substitution principle on the wall.<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[26],"tags":[25,23,77],"class_list":["post-797","post","type-post","status-publish","format-standard","hentry","category-programowanie","tag-java","tag-programowanie","tag-solid"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Zasada podstawiania Liskov &#8211; Wojciech Siwek<\/title>\n<meta name=\"description\" content=\"Trzeci w cyklu artyku\u0142 dotycz\u0105cy zasad SOLID. Tym razem na tapecie znajduje si\u0119 zasada podstawiania Liskov.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wojciechsiwek.pl\/en\/zasada-podstawiania-liskov\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Zasada podstawiania Liskov &#8211; Wojciech Siwek\" \/>\n<meta property=\"og:description\" content=\"Trzeci w cyklu artyku\u0142 dotycz\u0105cy zasad SOLID. Tym razem na tapecie znajduje si\u0119 zasada podstawiania Liskov.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wojciechsiwek.pl\/en\/zasada-podstawiania-liskov\/\" \/>\n<meta property=\"og:site_name\" content=\"Wojciech Siwek\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-21T19:58:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-02T10:13:08+00:00\" \/>\n<meta name=\"author\" content=\"Wojciech Siwek\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Wojciech Siwek\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/\"},\"author\":{\"name\":\"Wojciech Siwek\",\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\"},\"headline\":\"Zasada podstawiania Liskov\",\"datePublished\":\"2022-02-21T19:58:12+00:00\",\"dateModified\":\"2025-02-02T10:13:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/\"},\"wordCount\":257,\"publisher\":{\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\"},\"keywords\":[\"java\",\"programowanie\",\"SOLID\"],\"articleSection\":[\"Programowanie\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/\",\"url\":\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/\",\"name\":\"Zasada podstawiania Liskov &#8211; Wojciech Siwek\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#website\"},\"datePublished\":\"2022-02-21T19:58:12+00:00\",\"dateModified\":\"2025-02-02T10:13:08+00:00\",\"description\":\"Trzeci w cyklu artyku\u0142 dotycz\u0105cy zasad SOLID. Tym razem na tapecie znajduje si\u0119 zasada podstawiania Liskov.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/zasada-podstawiania-liskov\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Strona g\u0142\u00f3wna\",\"item\":\"https:\\\/\\\/wojciechsiwek.pl\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Zasada podstawiania Liskov\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#website\",\"url\":\"http:\\\/\\\/wojciechsiwek.pl\\\/\",\"name\":\"Wojciech Siwek\",\"description\":\"Programista na pocz\u0105tku swojej \u015bcie\u017cki w IT\",\"publisher\":{\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\\\/\\\/wojciechsiwek.pl\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\",\"name\":\"Wojciech Siwek\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\",\"url\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\",\"contentUrl\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\",\"width\":313,\"height\":306,\"caption\":\"Wojciech Siwek\"},\"logo\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\"},\"sameAs\":[\"https:\\\/\\\/wojciechsiwek.pl\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Zasada podstawiania Liskov &#8211; Wojciech Siwek","description":"The third article in the series about the SOLID principles. This time there is a Liskov substitution principle on the wall.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wojciechsiwek.pl\/en\/zasada-podstawiania-liskov\/","og_locale":"en_GB","og_type":"article","og_title":"Zasada podstawiania Liskov &#8211; Wojciech Siwek","og_description":"Trzeci w cyklu artyku\u0142 dotycz\u0105cy zasad SOLID. Tym razem na tapecie znajduje si\u0119 zasada podstawiania Liskov.","og_url":"https:\/\/wojciechsiwek.pl\/en\/zasada-podstawiania-liskov\/","og_site_name":"Wojciech Siwek","article_published_time":"2022-02-21T19:58:12+00:00","article_modified_time":"2025-02-02T10:13:08+00:00","author":"Wojciech Siwek","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Wojciech Siwek","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/#article","isPartOf":{"@id":"https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/"},"author":{"name":"Wojciech Siwek","@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677"},"headline":"Zasada podstawiania Liskov","datePublished":"2022-02-21T19:58:12+00:00","dateModified":"2025-02-02T10:13:08+00:00","mainEntityOfPage":{"@id":"https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/"},"wordCount":257,"publisher":{"@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677"},"keywords":["java","programowanie","SOLID"],"articleSection":["Programowanie"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/","url":"https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/","name":"Zasada podstawiania Liskov &#8211; Wojciech Siwek","isPartOf":{"@id":"http:\/\/wojciechsiwek.pl\/#website"},"datePublished":"2022-02-21T19:58:12+00:00","dateModified":"2025-02-02T10:13:08+00:00","description":"The third article in the series about the SOLID principles. This time there is a Liskov substitution principle on the wall.","breadcrumb":{"@id":"https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wojciechsiwek.pl\/zasada-podstawiania-liskov\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Strona g\u0142\u00f3wna","item":"https:\/\/wojciechsiwek.pl\/"},{"@type":"ListItem","position":2,"name":"Zasada podstawiania Liskov"}]},{"@type":"WebSite","@id":"http:\/\/wojciechsiwek.pl\/#website","url":"http:\/\/wojciechsiwek.pl\/","name":"Wojciech Siwek","description":"Software developer at the beginning of his career in IT","publisher":{"@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/wojciechsiwek.pl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677","name":"Wojciech Siwek","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png","url":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png","contentUrl":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png","width":313,"height":306,"caption":"Wojciech Siwek"},"logo":{"@id":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png"},"sameAs":["https:\/\/wojciechsiwek.pl"]}]}},"_links":{"self":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts\/797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/comments?post=797"}],"version-history":[{"count":9,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts\/797\/revisions"}],"predecessor-version":[{"id":806,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts\/797\/revisions\/806"}],"wp:attachment":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/media?parent=797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/categories?post=797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/tags?post=797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}