{"id":22262,"date":"2020-01-23T12:19:00","date_gmt":"2020-01-23T12:19:00","guid":{"rendered":"https:\/\/cnsfly.com\/vytcdc\/?p=22262"},"modified":"2024-09-26T11:47:40","modified_gmt":"2024-09-26T11:47:40","slug":"polymorphism-in-java","status":"publish","type":"post","link":"https:\/\/cnsfly.com\/vytcdc\/polymorphism-in-java\/","title":{"rendered":"Polymorphism in Java"},"content":{"rendered":"<p style=\"font-weight: 400;\">he word polymorphism means having many forms. In simple words,<\/p>\n<p style=\"font-weight: 400;\"><b><strong>Real life example of polymorphism:<\/strong><\/b>\u00a0A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person possess different behavior in different situations. This is called polymorphism.<\/p>\n<h3>Example<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class Animal{\r\n\r\n   \u2026\r\n\r\n   public void sound(){\r\n\r\n      System.out.println(\u201cAnimal is making a sound\u201d);  \r\n\r\n   }\r\n\r\n}<\/pre>\n<p>Now let\u2019s say we two subclasses of Animal class:\u00a0<code>Horse<\/code>\u00a0and\u00a0<code>Cat<\/code>\u00a0that extends (like\u00a0<a href=\"https:\/\/beginnersbook.com\/2013\/03\/inheritance-in-java\/\">Inheritance<\/a>)\u00a0<code>Animal<\/code>\u00a0class. We can provide the implementation to the same method like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public\u00a0class\u00a0Horse\u00a0extends\u00a0Animal{\r\n...\r\n@Override\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0void\u00a0sound(){\r\n     System.out.println(\"Neigh\");\r\n\u00a0\u00a0\u00a0\u00a0}\r\n}<\/pre>\n<p>and<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class Cat extends Animal{\r\n...\r\n @Override\r\npublic void sound(){\r\n    System.out.println(\"Meow\");\r\n    }\r\n}<\/pre>\n<p>We had the common action for all subclasses sound() Because of Animal class we get<br \/>\nsound() method in Horse and Cat, This is a perfect example of polymorphism, see the difference horse Neigh and cat Meow, each Animal has a different sound<\/p>\n<h3><strong>There are two types of polymorphism in java:<\/strong><\/h3>\n<p style=\"font-weight: 400;\"><b><strong>1) Static Polymorphism also known as compile time polymorphism<\/strong><\/b><\/p>\n<p style=\"font-weight: 400;\"><b><strong>2) Dynamic Polymorphism also known as runtime polymorphism<\/strong><\/b><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-23746 size-full\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly-1.png\" alt=\"\" width=\"1257\" height=\"544\" srcset=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly-1.png 1257w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly-1-300x130.png 300w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly-1-1024x443.png 1024w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly-1-768x332.png 768w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly-1-600x260.png 600w\" sizes=\"(max-width: 1257px) 100vw, 1257px\" \/>Compile time Polymorphism (or Static polymorphism)<\/p>\n<p>Polymorphism that is checked in compile time is known as static polymorphism<\/p>\n<p>Method overloading is an example of compile time polymorphism.<\/p>\n<p><strong>Method Overloading in Java<\/strong><\/p>\n<p>If a class has multiple methods having same name but different in parameters, it is known as Method <strong>Overloading<\/strong> in java.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class SimpleCalculator\r\n\r\n{\r\n    int add(int a, int b)\r\n    {\r\n         return a+b;\r\n    }\r\n    int  add(int a, int b, int c)  \r\n    {\r\n         return a+b+c;\r\n    }\r\n}\r\npublic class Demo\r\n{\r\n   public static void main(String args[])\r\n   {\r\n                       SimpleCalculator obj = new SimpleCalculator();\r\n       System.out.println(obj.add(10, 20));\r\n       System.out.println(obj.add(10, 20, 30));\r\n   }\r\n}<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Results\r\n\r\n30\r\n60<\/pre>\n<h3><b>Runtime Polymorphism (or Dynamic polymorphism)<\/b><b>\u00a0<\/b><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Employee{\r\n   public void profile(){\r\n                    System.out.println(\"Employee\");\r\n   }\r\n}\r\npublic class Contract extends Employee {\r\n \r\n   public void profile(){\r\n                    System.out.println(\"Employee of Contract\");\r\n   }\r\n}\r\npublic class Part_Time extends Employee {\r\n   public void profile(){\r\n                    System.out.println(\"Employee of Partime\");\r\n   }\r\n}<\/pre>\n<p>It is also known as late binding or Dynamic binding.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-23748 size-full\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly1.png\" alt=\"\" width=\"1295\" height=\"642\" srcset=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly1.png 1295w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly1-300x149.png 300w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly1-1024x508.png 1024w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly1-768x381.png 768w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly1-600x297.png 600w\" sizes=\"(max-width: 1295px) 100vw, 1295px\" \/><\/p>\n<p>In the above example while compile time s.draw() point to shape class.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-23749 size-full\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly2.png\" alt=\"\" width=\"1294\" height=\"642\" srcset=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly2.png 1294w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly2-300x149.png 300w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly2-1024x508.png 1024w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly2-768x381.png 768w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/01\/poly2-600x298.png 600w\" sizes=\"(max-width: 1294px) 100vw, 1294px\" \/><\/p>\n<p style=\"font-weight: 400;\">In the above example while compile time s.draw() point to shape class.\u00a0<a href=\"https:\/\/cnsfly.com\/vytcdc\/courses\/java-training-in-chennai\/\"><b><strong>Java training courses<\/strong><\/b><\/a><b><strong>\u00a0<\/strong><\/b>are clearly educate for java concepts.<\/p>\n<p style=\"font-weight: 400;\">\n","protected":false},"excerpt":{"rendered":"<p>The word polymorphism means having many forms. In simple words, Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same<\/p>\n","protected":false},"author":1,"featured_media":22857,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[76],"tags":[25,26],"class_list":["post-22262","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-design","tag-development"],"_links":{"self":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/comments?post=22262"}],"version-history":[{"count":1,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22262\/revisions"}],"predecessor-version":[{"id":33868,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22262\/revisions\/33868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media\/22857"}],"wp:attachment":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media?parent=22262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/categories?post=22262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/tags?post=22262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}