{"id":110,"date":"2026-03-02T08:35:55","date_gmt":"2026-03-02T03:35:55","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=110"},"modified":"2026-03-15T22:45:46","modified_gmt":"2026-03-15T17:45:46","slug":"polymorphism","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/","title":{"rendered":"Polymorphism"},"content":{"rendered":"\n<p>Polymorphism is one of the core principles of Object-Oriented Programming (OOP).<\/p>\n\n\n\n<p>The word <em>Polymorphism<\/em> means <strong>\u201cmany forms.\u201d<\/strong><\/p>\n\n\n\n<p>In programming, polymorphism allows the same method or function name to behave differently depending on the object that is using it.<\/p>\n\n\n\n<p>In simple words:<br>One name \u2192 Multiple behaviors<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Polymorphism is Important<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Increases flexibility<\/li>\n\n\n\n<li>Improves code reusability<\/li>\n\n\n\n<li>Makes code cleaner and more scalable<\/li>\n\n\n\n<li>Allows different objects to respond in their own way<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Polymorphism with Functions<\/strong><\/h2>\n\n\n\n<p>Different data types can use the same function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(len(\"Hira\"))<br>print(len([1, 2, 3, 4]))<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">4<br>4<\/pre>\n\n\n\n<p>The <code>len()<\/code> function works with strings and lists, but behaves according to the data type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Polymorphism with Method Overriding (Inheritance)<\/strong><\/h2>\n\n\n\n<p>A child class can change the behavior of a parent class method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Animal:<br>    def sound(self):<br>        print(\"Some sound\")class Dog(Animal):<br>    def sound(self):<br>        print(\"Bark\")class Cat(Animal):<br>    def sound(self):<br>        print(\"Meow\")d = Dog()<br>c = Cat()d.sound()<br>c.sound()<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Bark<br>Meow<\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Same method name <code>sound()<\/code><\/li>\n\n\n\n<li>Different behavior depending on the object<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Polymorphism Using Same Method Name in Different Classes<\/strong><\/h2>\n\n\n\n<p>Even without inheritance, different classes can have the same method name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Bird:<br>    def move(self):<br>        print(\"Flying\")class Fish:<br>    def move(self):<br>        print(\"Swimming\")b = Bird()<br>f = Fish()b.move()<br>f.move()<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Flying<br>Swimming<\/pre>\n\n\n\n<p>This is called <strong>Duck Typing<\/strong> in Python.<\/p>\n\n\n\n<p>If it behaves like a duck, Python treats it like a duck.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Polymorphism in Python<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Compile-time Polymorphism<br>Achieved through method overloading<br>(Python does not support traditional overloading, but default arguments can simulate it)<\/li>\n\n\n\n<li>Runtime Polymorphism<br>Achieved through method overriding<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example of Simulated Method Overloading<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class Math:<br>    def add(self, a, b=0):<br>        return a + bm = Math()<br>print(m.add(5))<br>print(m.add(5, 3))<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Takeaway<\/strong><\/h2>\n\n\n\n<p>Polymorphism allows one interface to be used for different data types or objects.<\/p>\n\n\n\n<p>It helps make programs flexible, reusable, and easy to extend in Object-Oriented Programming.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Polymorphism<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773596543669\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":53,"template":"","class_list":["post-110","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Polymorphism - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn polymorphism in Python OOP with examples of method overriding, duck typing, and flexible functions for reusable code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Polymorphism - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn polymorphism in Python OOP with examples of method overriding, duck typing, and flexible functions for reusable code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T17:45:46+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/polymorphism\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/polymorphism\\\/\",\"name\":\"Polymorphism - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:35:55+00:00\",\"dateModified\":\"2026-03-15T17:45:46+00:00\",\"description\":\"Learn polymorphism in Python OOP with examples of method overriding, duck typing, and flexible functions for reusable code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/polymorphism\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/polymorphism\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/polymorphism\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Polymorphism\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Polymorphism - One Language. Endless Possibilities","description":"Learn polymorphism in Python OOP with examples of method overriding, duck typing, and flexible functions for reusable code.","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:\/\/gigz.pk\/python\/lesson\/polymorphism\/","og_locale":"en_US","og_type":"article","og_title":"Polymorphism - One Language. Endless Possibilities","og_description":"Learn polymorphism in Python OOP with examples of method overriding, duck typing, and flexible functions for reusable code.","og_url":"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-15T17:45:46+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/","url":"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/","name":"Polymorphism - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:35:55+00:00","dateModified":"2026-03-15T17:45:46+00:00","description":"Learn polymorphism in Python OOP with examples of method overriding, duck typing, and flexible functions for reusable code.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/polymorphism\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/polymorphism\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Polymorphism"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}