                                        {"id":7042,"date":"2024-04-03T18:18:56","date_gmt":"2024-04-03T18:18:56","guid":{"rendered":"https:\/\/eservices360.com\/?p=7042"},"modified":"2024-04-03T18:23:38","modified_gmt":"2024-04-03T18:23:38","slug":"understanding-polymorphism-in-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/eservices360.com\/resources\/understanding-polymorphism-in-object-oriented-programming\/","title":{"rendered":"Understanding Polymorphism in Object-Oriented Programming"},"content":{"rendered":"<div>\n<h2 id=\"3e32\" class=\"pw-subtitle-paragraph hr gt gu be b hs ht hu hv hw hx hy hz ia ib ic id ie if ig cp dt\">How polymorphism makes the code better<\/h2>\n<\/div>\n<p id=\"4131\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">In computer science, polymorphism describes the idea you can entry objects of various sorts by the identical interface. In less complicated phrases, it may be outlined as having a distant management with a single button that does various things relying on what you\u2019re pointing it at.<\/p>\n<p id=\"b2c5\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">In this article, we are going to examine a code with out the idea of polymorphism after which talk about how making use of the idea of polymorphism could make the code higher. Nicely first take a look at the next code with out polymorphism and attempt to perceive what are the issues with this code:<\/p>\n<pre class=\"nw nx ny nz oa oh oi oj bo ok ba bj\"><span class=\"ol om gu oi b bf on oo l op oq\">&lt;?php\n<\/span>class Product {\nprotected $identify;\nprotected $worth;\npublic operate __construct($identify, $worth) \n$this-&gt;identify = $identify;\n$this-&gt;worth = $worth;\n\npublic operate displayDetails($kind, $additional) {\nif ($kind == \"clothing\") {\necho \"Clothing: {$this-&gt;name}, Size: {$extra}, Price: {$this-&gt;price}\\n\";\n} elseif ($kind == \"electronics\") {\necho \"Electronics: {$this-&gt;name}, Brand: {$extra}, Price: {$this-&gt;price}\\n\";\n}\n}\n}\n\/\/ Create new product objects\n$product_cloth = new Product(\"T-Shirt\", 19.99);\n$product_electronics = new Product(\"iPhone15\", 199.99);\n\/\/ Show particulars for clothes with measurement XL\n$product-&gt;displayDetails(\"clothing\", \"XL\");\n\/\/ Show particulars for electronics with model iPhone\n$product-&gt;displayDetails(\"electronics\", \"iPhone\");\n?&gt;<\/pre>\n<p id=\"3bb3\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">Now, there are a number of issues with this code. To begin with, if we wish to add one other product kind, let\u2019s say <code class=\"cw or os ot oi b\">guide<\/code> we might want to add yet another if situation within the product class. Nonetheless, a category ought to be capable to be prolonged flexibly with out modifying the category itself.<\/p>\n<p id=\"fbce\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">One other drawback with this code is, for instance, identify and worth are widespread params for each electronics kind of merchandise and clothes kind of merchandise. However for instance, there may be some unusual attribute for various kinds of merchandise, which on this case was applied by including one other param named <code class=\"cw or os ot oi b\">additional<\/code> . But when there are 10 such attributes, you possibly can simply guess how messy the code shall be.<\/p>\n<p id=\"19e4\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">So, to do away with such issues, we will rewrite the code utilizing the idea of polymorphism like this:<\/p>\n<pre class=\"nw nx ny nz oa oh oi oj bo ok ba bj\"><span class=\"ol om gu oi b bf on oo l op oq\">&lt;?php\n<\/span>class Product {\nprotected $identify;\nprotected $worth;\n\npublic operate __construct($identify, $worth) \n$this-&gt;identify = $identify;\n$this-&gt;worth = $worth;\n\npublic operate displayDetails() {\necho \"Product: {$this-&gt;name}, Price: {$this-&gt;price}\\n\";\n}\n}\nclass Guide extends Product {\nnon-public $creator;\n\npublic operate __construct($identify, $worth, $creator) \nguardian::__construct($identify, $worth);\n$this-&gt;creator = $creator;\n\npublic operate displayDetails() {\necho \"Book: {$this-&gt;name}, Author: {$this-&gt;author}, Price: {$this-&gt;price}\\n\";\n}\n}\n\nclass Electronics extends Product {\nnon-public $model;\npublic operate __construct($identify, $worth, $model) \nguardian::__construct($identify, $worth);\n$this-&gt;model = $model;\n\npublic operate displayDetails() {\necho \"Electronics: {$this-&gt;name}, Brand: {$this-&gt;brand}, Price: {$this-&gt;price}\\n\";\n}\n}\n\nclass Clothes extends Product {\nnon-public $measurement;\n\npublic operate __construct($identify, $worth, $measurement)\n\npublic operate displayDetails() {\necho \"Clothing: {$this-&gt;name}, Size: {$this-&gt;size}, Price: {$this-&gt;price}\\n\";\n}\n}\n\n\/\/ Operate to show product particulars\noperate displayProductDetails(Product $product) {\n$product-&gt;displayDetails();\n}\n\n\/\/ Creating objects\n$guide = new Guide(\"The Great Gatsby\", 15.99, \"F. Scott Fitzgerald\");\n$electronics = new Electronics(\"Smartphone\", 499.99, \"Samsung\");\n$clothes = new Clothes(\"T-Shirt\", 19.99, \"M\");\n\n\/\/ Displaying particulars utilizing polymorphism\ndisplayProductDetails($guide);\ndisplayProductDetails($electronics);\ndisplayProductDetails($clothes);\n?&gt;<\/pre>\n<p id=\"9e51\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">The benefit of this code is, to start with, you possibly can maintain including extra varieties of merchandise simply by extending the Product class.<\/p>\n<p id=\"625f\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">Then, for various kinds of merchandise, you possibly can add any attributes with out affecting the guardian class. Simply overriding the displayDetails() operate and implementing it as wanted will do it.<\/p>\n<p id=\"2c73\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">Additionally if you happen to take a look at the `displayProductDetails` operate, you possibly can see that we will deal with all of the various kinds of merchandise the identical approach. we will name the identical operate nevertheless it provides various kinds of outcomes for various kinds of merchandise.<\/p>\n<p id=\"feed\" class=\"pw-post-body-paragraph mx my gu mz b hs pp nb nc hv pq ne nf ng pr ni nj nk ps nm nn no pt nq nr ns gn bj\">Static polymorphism, often known as compile-time polymorphism, happens when the strategy to be invoked is decided at compile time. In PHP, technique overloading is a type of static polymorphism. Methodology overloading permits a category to have a number of strategies with the identical identify however with completely different parameters or argument lists.<\/p>\n<p id=\"64b9\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">Right here\u2019s an instance of technique overloading in PHP:<\/p>\n<pre class=\"nw nx ny nz oa oh oi oj bo ok ba bj\"><span id=\"2818\" class=\"ol om gu oi b bf on oo l op oq\">&lt;?php\nclass MathOperations {\n\/\/ Methodology so as to add two numbers\npublic operate add($num1, $num2) {\nreturn $num1 + $num2;\n}\n\/\/ Methodology overloading so as to add three numbers\npublic operate add($num1, $num2, $num3) {\nreturn $num1 + $num2 + $num3;\n}\n}\n$math = new MathOperations();\necho $math-&gt;add(2, 3) . \"\\n\";         \/\/ Output: 5\necho $math-&gt;add(2, 3, 4) . \"\\n\";      \/\/ Output: 9\n?&gt;<\/span><\/pre>\n<p id=\"f449\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">Within the above instance, the <code class=\"cw or os ot oi b\">MathOperations<\/code> class has two <code class=\"cw or os ot oi b\">add()<\/code> strategies. The primary one takes two parameters, and the second takes three parameters. The PHP interpreter decides which technique to name primarily based on the variety of arguments supplied throughout the operate name. That is decided at compile time.<\/p>\n<p id=\"6c6b\" class=\"pw-post-body-paragraph mx my gu mz b hs pp nb nc hv pq ne nf ng pr ni nj nk ps nm nn no pt nq nr ns gn bj\">Dynamic polymorphism, often known as runtime polymorphism, happens when the strategy to be invoked is decided at runtime. In PHP, technique overriding in subclass is a type of dynamic polymorphism. Methodology overriding permits a subclass to offer a particular implementation of a technique that&#8217;s already outlined in its superclass. The primary polymorphism instance which was given on this article is an instance of dynamic polymorphism.<\/p>\n<p id=\"04f8\" class=\"pw-post-body-paragraph mx my gu mz b hs na nb nc hv nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gn bj\">So, that\u2019s it. From this dialogue we noticed how the idea of polymorphism elevated the code\u2019s high quality by giving higher abstraction, lowering code duplication and enhancing flexibility and extensibility of the the category. This makes it simpler so as to add new varieties of objects or modify current ones with out having to alter plenty of code. Completely happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How polymorphism makes the code better In computer science, polymorphism describes the idea you can entry objects of various sorts by the identical interface. In less complicated phrases, it may be outlined as having a distant management with a single button that does various things relying on what you\u2019re pointing it at. In this article, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7043,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94],"tags":[109,101,108,111,107,96,103],"class_list":["post-7042","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-codeigniter","tag-php-debugging","tag-php-deployment","tag-php-frameworks","tag-php-performance-optimization","tag-server-side-scripting","tag-symfony"],"blog_post_layout_featured_media_urls":{"thumbnail":["https:\/\/eservices360.com\/resources\/wp-content\/uploads\/2024\/04\/1xTIRTPPn9bdLAOWP_u3pUw-150x150.jpeg",150,150,true],"full":["https:\/\/eservices360.com\/resources\/wp-content\/uploads\/2024\/04\/1xTIRTPPn9bdLAOWP_u3pUw.jpeg",1200,758,false]},"categories_names":{"94":{"name":"PHP","link":"https:\/\/eservices360.com\/resources\/category\/php\/"}},"tags_names":{"109":{"name":"Codeigniter","link":"https:\/\/eservices360.com\/resources\/tag\/codeigniter\/"},"101":{"name":"PHP Debugging","link":"https:\/\/eservices360.com\/resources\/tag\/php-debugging\/"},"108":{"name":"PHP Deployment","link":"https:\/\/eservices360.com\/resources\/tag\/php-deployment\/"},"111":{"name":"PHP Frameworks","link":"https:\/\/eservices360.com\/resources\/tag\/php-frameworks\/"},"107":{"name":"PHP Performance Optimization","link":"https:\/\/eservices360.com\/resources\/tag\/php-performance-optimization\/"},"96":{"name":"Server-Side Scripting","link":"https:\/\/eservices360.com\/resources\/tag\/server-side-scripting\/"},"103":{"name":"Symfony","link":"https:\/\/eservices360.com\/resources\/tag\/symfony\/"}},"comments_number":"0","_links":{"self":[{"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/posts\/7042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/comments?post=7042"}],"version-history":[{"count":3,"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/posts\/7042\/revisions"}],"predecessor-version":[{"id":7191,"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/posts\/7042\/revisions\/7191"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/media\/7043"}],"wp:attachment":[{"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/media?parent=7042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/categories?post=7042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eservices360.com\/resources\/wp-json\/wp\/v2\/tags?post=7042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}