{"id":132,"date":"2026-05-20T11:13:52","date_gmt":"2026-05-20T11:13:52","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=132"},"modified":"2026-05-21T14:40:40","modified_gmt":"2026-05-21T14:40:40","slug":"polymorphism","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=polymorphism","title":{"rendered":"Polymorphism"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction to Polymorphism<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism is one of the main concepts of Object-Oriented Programming (OOP). The word polymorphism means \u201cmany forms.\u201d In programming, polymorphism allows one method, function, or object to behave differently depending on the situation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism helps developers create flexible, reusable, and maintainable code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand the concept of polymorphism<\/li>\n\n\n\n<li>Learn the types of polymorphism<\/li>\n\n\n\n<li>Use polymorphism in object-oriented programming<\/li>\n\n\n\n<li>Differentiate between method overloading and method overriding<\/li>\n\n\n\n<li>Build reusable and scalable applications<\/li>\n\n\n\n<li>Apply polymorphism in real-world projects<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is Polymorphism<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism allows the same function or method name to perform different tasks based on the object or input.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A person can behave as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Student<\/li>\n\n\n\n<li>Teacher<\/li>\n\n\n\n<li>Employee<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The same person performs different actions depending on the role. Similarly, in programming, one method can behave differently in different classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Polymorphism<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Compile Time Polymorphism<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This type of polymorphism occurs during program compilation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is usually achieved through:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Method Overloading<\/li>\n\n\n\n<li>Operator Overloading<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Run Time Polymorphism<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This type occurs while the program is running.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is commonly achieved through:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Method Overriding<\/li>\n\n\n\n<li>Inheritance<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Method Overloading<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method overloading allows multiple methods with the same name but different parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example in PHP:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Calculator {<br><br>    function add($a, $b, $c = 0) {<br>        return $a + $b + $c;<br>    }<br>}<br><br>$calc = new Calculator();<br><br>echo $calc-&gt;add(10, 20);<br>echo $calc-&gt;add(10, 20, 30);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Method Overriding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method overriding occurs when a child class provides a different implementation of a method already defined in the parent class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example in PHP:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Animal {<br>    public function sound() {<br>        echo \"Animal makes a sound\";<br>    }<br>}<br><br>class Dog extends Animal {<br>    public function sound() {<br>        echo \"Dog barks\";<br>    }<br>}<br><br>$obj = new Dog();<br>$obj-&gt;sound();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Polymorphism<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Code Reusability<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The same method can be reused in different ways.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Flexibility<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Programs become more adaptable and scalable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Easy Maintenance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Changes can be made without affecting the entire system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Improved Readability<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Code becomes cleaner and easier to understand.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a payment system:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Credit Card Payment<\/li>\n\n\n\n<li>PayPal Payment<\/li>\n\n\n\n<li>Bank Transfer<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All payment methods use the same process method but behave differently internally.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is polymorphism in action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Polymorphism in Object-Oriented Programming<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism works closely with:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Classes<\/li>\n\n\n\n<li>Objects<\/li>\n\n\n\n<li>Inheritance<\/li>\n\n\n\n<li>Encapsulation<\/li>\n\n\n\n<li>Abstraction<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It is a key feature used in modern software development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Polymorphism<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduces code duplication<\/li>\n\n\n\n<li>Improves program flexibility<\/li>\n\n\n\n<li>Supports scalability<\/li>\n\n\n\n<li>Simplifies complex systems<\/li>\n\n\n\n<li>Encourages reusable programming practices<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Disadvantages of Polymorphism<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Can increase complexity for beginners<\/li>\n\n\n\n<li>Debugging may become difficult in large systems<\/li>\n\n\n\n<li>Requires proper class design<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use meaningful method names<\/li>\n\n\n\n<li>Keep methods simple and focused<\/li>\n\n\n\n<li>Avoid unnecessary overriding<\/li>\n\n\n\n<li>Follow object-oriented design principles<\/li>\n\n\n\n<li>Test polymorphic behavior carefully<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Career Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism is widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web Development<\/li>\n\n\n\n<li>Software Engineering<\/li>\n\n\n\n<li>Mobile Applications<\/li>\n\n\n\n<li>Game Development<\/li>\n\n\n\n<li>Enterprise Systems<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding polymorphism is essential for becoming a professional programmer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Presentation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In your final presentation, explain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Definition of polymorphism<\/li>\n\n\n\n<li>Types of polymorphism<\/li>\n\n\n\n<li>Method overloading and overriding<\/li>\n\n\n\n<li>Real-world examples<\/li>\n\n\n\n<li>Benefits of polymorphism<\/li>\n\n\n\n<li>Importance in object-oriented programming<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced PHP > Object-Oriented PHP > Polymorphism<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779275629069\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":40,"template":"","class_list":["post-132","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Polymorphism - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn polymorphism in OOP with examples of method overriding, overloading, inheritance, and reusable coding concepts.\" \/>\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\/php\/?lesson=polymorphism\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Polymorphism - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn polymorphism in OOP with examples of method overriding, overloading, inheritance, and reusable coding concepts.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=polymorphism\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:40:40+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=polymorphism\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=polymorphism\",\"name\":\"Polymorphism - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T11:13:52+00:00\",\"dateModified\":\"2026-05-21T14:40:40+00:00\",\"description\":\"Learn polymorphism in OOP with examples of method overriding, overloading, inheritance, and reusable coding concepts.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=polymorphism#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=polymorphism\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=polymorphism#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced PHP > Object-Oriented PHP > Polymorphism\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?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 - Learn PHP with GiGz.PK","description":"Learn polymorphism in OOP with examples of method overriding, overloading, inheritance, and reusable coding concepts.","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\/php\/?lesson=polymorphism","og_locale":"en_US","og_type":"article","og_title":"Polymorphism - Learn PHP with GiGz.PK","og_description":"Learn polymorphism in OOP with examples of method overriding, overloading, inheritance, and reusable coding concepts.","og_url":"https:\/\/gigz.pk\/php\/?lesson=polymorphism","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:40:40+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=polymorphism","url":"https:\/\/gigz.pk\/php\/?lesson=polymorphism","name":"Polymorphism - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T11:13:52+00:00","dateModified":"2026-05-21T14:40:40+00:00","description":"Learn polymorphism in OOP with examples of method overriding, overloading, inheritance, and reusable coding concepts.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=polymorphism#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=polymorphism"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=polymorphism#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Advanced PHP > Object-Oriented PHP > Polymorphism"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}