{"id":334,"date":"2025-06-30T10:00:32","date_gmt":"2025-06-30T10:00:32","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=334"},"modified":"2025-07-16T10:10:37","modified_gmt":"2025-07-16T10:10:37","slug":"what-is-polymorphism","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-polymorphism\/","title":{"rendered":"What is Polymorphism?"},"content":{"rendered":"\n<p>\ud83d\udd04 <strong>What Is Polymorphism?<\/strong><\/p>\n\n\n\n<p>Polymorphism simply means <strong>\u201cmany forms.\u201d<\/strong> In Java, it lets the same method or object behave in <strong>different ways<\/strong> depending on the situation.<\/p>\n\n\n\n<p><strong>\ud83d\udca1 Think of it like this:<\/strong><\/p>\n\n\n\n<p>Imagine a single remote control that can operate a <strong>TV<\/strong>, a <strong>speaker<\/strong>, or an <strong>AC<\/strong>. It\u2019s the same remote, but depending on which device it\u2019s controlling, the function changes. That\u2019s polymorphism in action.<\/p>\n\n\n\n<p>There are <strong>two main types<\/strong>:<\/p>\n\n\n\n<p>1. <strong>Compile-Time Polymorphism<\/strong> (Static Binding)<\/p>\n\n\n\n<p>Achieved through <strong>method overloading<\/strong>\u2014same method name, different parameter lists.<\/p>\n\n\n\n<p>2. <strong>Runtime Polymorphism<\/strong> (Dynamic Binding)<\/p>\n\n\n\n<p>Achieved through <strong>method overriding<\/strong>\u2014a subclass provides a specific implementation of a method already defined in its superclass.<\/p>\n\n\n\n<p>\ud83e\uddea <strong>1. Compile-Time Polymorphism (Method Overloading)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Printer {\n    void print(String msg) {\n        System.out.println(\"Message: \" + msg);\n    }\n    void print(int number) {\n        System.out.println(\"Number: \" + number);\n    }\n}\n\n\/\/ Usage\nPrinter p = new Printer();\np.print(\"Hello\");  \/\/ Message: Hello\np.print(123);      \/\/ Number: 123<\/code><\/pre>\n\n\n\n<p><strong>Usage:<\/strong><\/p>\n\n\n\n<p>Printer p = new Printer(); p.print(&#8220;Hello&#8221;); \/\/ Message: Hello p.print(123); \/\/ Number: 123<\/p>\n\n\n\n<p>The compiler decides which method to call based on the argument type.<\/p>\n\n\n\n<p>\ud83e\uddea<strong> 2. Runtime Polymorphism (Method Overriding)<\/strong><\/p>\n\n\n\n<p>class Animal {<br>void makeSound() {<br>System.out.println(&#8220;Animal makes a sound&#8221;);<br>}<br>}<\/p>\n\n\n\n<p>class Dog extends Animal {<br>@Override<br>void makeSound() {<br>System.out.println(&#8220;Dog barks&#8221;);<br>}<br>}<\/p>\n\n\n\n<p>\/\/ Usage<br>Animal a = new Dog();<br>a.makeSound(); \/\/ Output: Dog barks<\/p>\n\n\n\n<p><strong>Usage:<\/strong><\/p>\n\n\n\n<p>Animal a = new Dog(); <\/p>\n\n\n\n<p>a.makeSound(); \/\/ Output: Dog barks<\/p>\n\n\n\n<p>Even though the reference is of type <code>Animal<\/code>, the actual method called is from <code>Dog<\/code>\u2014decided at runtime.<\/p>\n\n\n\n<p><strong>\ud83e\udde0 Why Use Polymorphism?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Code Reusability<\/strong>: Write generic code that works with multiple types.<\/li>\n\n\n\n<li><strong>Flexibility<\/strong>: Add new behaviors without changing existing code.<\/li>\n\n\n\n<li><strong>Maintainability<\/strong>: Cleaner, more modular code.<\/li>\n<\/ul>\n\n\n\n<p><strong>\ud83c\udf0d Real-World Examples of Polymorphism<\/strong><\/p>\n\n\n\n<p>\u2705 1. <strong>Payment Processing System<\/strong><\/p>\n\n\n\n<p>interface Payment { void pay(double amount); } class CreditCard implements Payment { public void pay(double amount) { System.out.println(&#8220;Paid \u20b9&#8221; + amount + &#8221; using Credit Card&#8221;); } } class UPI implements Payment { public void pay(double amount) { System.out.println(&#8220;Paid \u20b9&#8221; + amount + &#8221; using UPI&#8221;); } }<\/p>\n\n\n\n<p><strong>Usage:<\/strong><\/p>\n\n\n\n<p>Payment payment = new UPI(); payment.pay(500); \/\/ Output: Paid \u20b9500 using UPI<\/p>\n\n\n\n<p>The same <code>pay()<\/code> method behaves differently depending on the payment method.<\/p>\n\n\n\n<p>\u2705 2. <strong>Media Player<\/strong><\/p>\n\n\n\n<p>A <code>MediaPlayer<\/code> class might have a method <code>play()<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>AudioPlayer<\/code> plays MP3s<\/li>\n\n\n\n<li><code>VideoPlayer<\/code> plays MP4s<\/li>\n\n\n\n<li><code>StreamingPlayer<\/code> plays online content<\/li>\n<\/ul>\n\n\n\n<p>All override the same <code>play()<\/code> method, but the behavior changes based on the media type.<\/p>\n\n\n\n<p>\u2705 3. <strong>Human Roles<\/strong><\/p>\n\n\n\n<p>A person can be:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>teacher<\/strong> in school<\/li>\n\n\n\n<li>A <strong>parent<\/strong> at home<\/li>\n\n\n\n<li>A <strong>customer<\/strong> in a store<\/li>\n<\/ul>\n\n\n\n<p>Same person, different behaviors depending on the context\u2014just like polymorphism!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore polymorphism in Java and its role in object-oriented programming. Learn how it enables method overriding, flexibility, and dynamic behavior in code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions\/423"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}