{"id":87,"date":"2017-01-22T07:50:02","date_gmt":"2017-01-22T07:50:02","guid":{"rendered":"http:\/\/learntowish.com\/?p=87"},"modified":"2017-01-22T07:50:02","modified_gmt":"2017-01-22T07:50:02","slug":"polymorphism-in-java","status":"publish","type":"post","link":"https:\/\/learnscripting.org\/polymorphism-in-java\/","title":{"rendered":"Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism"},"content":{"rendered":"<p><span lang=\"EN-US\">Polymorphism in the java as the word poly means &#8220;More than one&#8221; and morph ism means &#8220;Functionality&#8221;\u00a0 is also applicable in java,where the function behave the more than one way, With same function name and different parameter.<\/span><\/p>\n<p><span lang=\"EN-US\"><br \/>\n<\/span>For instance, let\u2019s consider a class <code class=\" language-undefined\">Animal<\/code> and let <code class=\" language-undefined\">Cat<\/code> be a subclass of <code class=\" language-undefined\">Animal<\/code>. So, any cat <strong>IS<\/strong> animal. Here, <code class=\" language-undefined\">Cat<\/code> satisfies the IS-A relationship for its own type as well as its super class <code class=\" language-undefined\">Animal<\/code>.<\/p>\n<p><strong>Note:<\/strong> It\u2019s also legal to say every object in Java is polymorphic in nature, as each one passes an IS-A test for itself and also for <code class=\" language-undefined\">Object<\/code> class.<\/p>\n<h2>Static Polymorphism\/<span lang=\"EN-US\">Compile time polymorphism<\/span>:<\/h2>\n<p>In Java, static polymorphism is achieved through method overloading. Method overloading means there are several methods present in a class having the same name but different types\/order\/number of parameters.<\/p>\n<p>At compile time, Java knows which method to invoke by checking the method signatures. \u00a0So, this is called <strong>compile time polymorphism<\/strong> or <strong>static binding<\/strong>. The concept will be clear from the following example:<\/p>\n<pre class=\" language-undefined\"><code class=\" language-undefined\">class DemoOverload{\n\n    public int add(int x, int y){\u00a0 \/\/method 1\n\n    return x+y;\n\n    }\n\n    public int add(int x, int y, int z){ \/\/method 2\n\n    return x+y+z;\n\n    }\n\n    public int add(double x, int y){ \/\/method 3\n\n    return (int)x+y;\n\n    }\n\n    public int add(int x, double y){ \/\/method 4\n\n    return x+(int)y;\n\n    }\n\n}\n\nclass Test{\n\n    public static void main(String[] args){\n\n    DemoOverload demo=new DemoOverload();\n\n    System.out.println(demo.add(2,3));\u00a0\u00a0\u00a0 \u00a0\u00a0\/\/method 1 called\n\n    System.out.println(demo.add(2,3,4));\u00a0\u00a0\u00a0 \/\/method 2 called\n\n    System.out.println(demo.add(2,3.4));\u00a0\u00a0\u00a0 \/\/method 4 called\n\n    System.out.println(demo.add(2.5,3));\u00a0\u00a0\u00a0 \/\/method 3 called\n\n    }\n\n}<\/code><\/pre>\n<p>In the above example, there are four versions of <code class=\" language-undefined\">add<\/code> methods. The first method takes two parameters while the second one takes three. For the third and fourth methods there is a change of order of parameters. \u00a0The compiler looks at the method signature and decides which method to invoke for a particular method call at compile time.<\/p>\n<h2>Dynamic Polymorphism\/<span lang=\"EN-US\">Run time Ploymorphism<\/span>:<\/h2>\n<p>Suppose a sub class overrides a particular method of the super class. Let\u2019s say, in the program we create an object of the subclass and assign it to the super class reference. Now, if we call the overridden method on the super class reference then the sub class version of the method will be called.<\/p>\n<p>Have a look at the following example.<\/p>\n<pre class=\" language-undefined\"><code class=\" language-undefined\">class Vehicle{\n\n    public void move(){\n\n    System.out.println(\u201cVehicles can move!!\u201d);\n\n    }\n\n}\n\nclass MotorBike extends Vehicle{\n\n    public void move(){\n\n    System.out.println(\u201cMotorBike can move and accelerate too!!\u201d);\n\n    }\n\n}\n\nclass Test{\n\n    public static void main(String[] args){\n\n    Vehicle vh=new MotorBike();\n\n    vh.move();\u00a0\u00a0\u00a0 \/\/ prints MotorBike can move and accelerate too!!\n\n    vh=new Vehicle();\n\n    vh.move();\u00a0\u00a0\u00a0 \/\/ prints Vehicles can move!!\n\n    }\n\n}<\/code><\/pre>\n<p>It should be noted that in the first call to <code class=\" language-undefined\">move()<\/code>, the reference type is <code class=\" language-undefined\">Vehicle<\/code> and the object being referenced is <code class=\" language-undefined\">MotorBike<\/code>. So, when a call to <code class=\" language-undefined\">move()<\/code> is made, Java waits until runtime to determine which object is actually being pointed to by the reference. \u00a0In this case, the object is of the class <code class=\" language-undefined\">MotorBike<\/code>. So, the <code class=\" language-undefined\">move()<\/code> method of <code class=\" language-undefined\">MotorBike<\/code> class will be called. In the second call to <code class=\" language-undefined\">move()<\/code>, the object is of the class <code class=\" language-undefined\">Vehicle<\/code>. So, the <code class=\" language-undefined\">move()<\/code> method of <code class=\" language-undefined\">Vehicle<\/code> will be called.<\/p>\n<p>As the method to call is determined at runtime, this is called <strong>dynamic binding<\/strong> or <strong>late binding<\/strong>.<\/p>\n<h3>If you have any further clarification on this topic please give comment below post Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism in the java as the word poly means &#8220;More than one&#8221; and morph ism means &#8220;Functionality&#8221;\u00a0 is also applicable in java,where the function behave the more than one way, With same function name and different parameter. For instance, let\u2019s consider a class Animal and let Cat be a subclass of Animal. So, any cat &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1057,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":{"facebook_2613112545524186_272996453239123":"","twitter_aTo5NzY3MTIyNTIwMzEwNTM4MjQ7_976712252031053800":""},"rop_publish_now_history":[],"rop_publish_now_status":"pending","footnotes":""},"categories":[17],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism - Learn Scripting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnscripting.org\/polymorphism-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism - Learn Scripting\" \/>\n<meta property=\"og:description\" content=\"Polymorphism in the java as the word poly means &#8220;More than one&#8221; and morph ism means &#8220;Functionality&#8221;\u00a0 is also applicable in java,where the function behave the more than one way, With same function name and different parameter. For instance, let\u2019s consider a class Animal and let Cat be a subclass of Animal. So, any cat &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnscripting.org\/polymorphism-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Scripting\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/profile.php?id=100064270142636\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-22T07:50:02+00:00\" \/>\n<meta name=\"author\" content=\"Shakti Das\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shakti Das\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/\"},\"author\":{\"name\":\"Shakti Das\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/person\\\/71045e0c38b97ea7f76363d6effa320c\"},\"headline\":\"Polymorphism in Java Static Polymorphism\\\/Compile time polymorphism vs Dynamic Polymorphism\\\/Run time Ploymorphism\",\"datePublished\":\"2017-01-22T07:50:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/\"},\"wordCount\":430,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/\",\"name\":\"Polymorphism in Java Static Polymorphism\\\/Compile time polymorphism vs Dynamic Polymorphism\\\/Run time Ploymorphism - Learn Scripting\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"datePublished\":\"2017-01-22T07:50:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#primaryimage\",\"url\":\"\",\"contentUrl\":\"\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/polymorphism-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/learnscripting.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Polymorphism in Java Static Polymorphism\\\/Compile time polymorphism vs Dynamic Polymorphism\\\/Run time Ploymorphism\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#website\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/\",\"name\":\"Learn Scripting\",\"description\":\"Coding Knowledge Unveiled: Empower Yourself\",\"publisher\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/learnscripting.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\",\"name\":\"Learn Scripting\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/learnscripting.org\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/learnscripting.org\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1\",\"width\":512,\"height\":512,\"caption\":\"Learn Scripting\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100064270142636\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/person\\\/71045e0c38b97ea7f76363d6effa320c\",\"name\":\"Shakti Das\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"caption\":\"Shakti Das\"},\"sameAs\":[\"https:\\\/\\\/learnscripting.org\"],\"url\":\"https:\\\/\\\/learnscripting.org\\\/author\\\/53kgl\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism - Learn Scripting","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:\/\/learnscripting.org\/polymorphism-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism - Learn Scripting","og_description":"Polymorphism in the java as the word poly means &#8220;More than one&#8221; and morph ism means &#8220;Functionality&#8221;\u00a0 is also applicable in java,where the function behave the more than one way, With same function name and different parameter. For instance, let\u2019s consider a class Animal and let Cat be a subclass of Animal. So, any cat &hellip;","og_url":"https:\/\/learnscripting.org\/polymorphism-in-java\/","og_site_name":"Learn Scripting","article_publisher":"https:\/\/www.facebook.com\/profile.php?id=100064270142636","article_published_time":"2017-01-22T07:50:02+00:00","author":"Shakti Das","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Shakti Das","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/#article","isPartOf":{"@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/"},"author":{"name":"Shakti Das","@id":"https:\/\/learnscripting.org\/#\/schema\/person\/71045e0c38b97ea7f76363d6effa320c"},"headline":"Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism","datePublished":"2017-01-22T07:50:02+00:00","mainEntityOfPage":{"@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/"},"wordCount":430,"commentCount":1,"publisher":{"@id":"https:\/\/learnscripting.org\/#organization"},"image":{"@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/learnscripting.org\/polymorphism-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/","url":"https:\/\/learnscripting.org\/polymorphism-in-java\/","name":"Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism - Learn Scripting","isPartOf":{"@id":"https:\/\/learnscripting.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/#primaryimage"},"image":{"@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"","datePublished":"2017-01-22T07:50:02+00:00","breadcrumb":{"@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnscripting.org\/polymorphism-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/learnscripting.org\/polymorphism-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnscripting.org\/"},{"@type":"ListItem","position":2,"name":"Polymorphism in Java Static Polymorphism\/Compile time polymorphism vs Dynamic Polymorphism\/Run time Ploymorphism"}]},{"@type":"WebSite","@id":"https:\/\/learnscripting.org\/#website","url":"https:\/\/learnscripting.org\/","name":"Learn Scripting","description":"Coding Knowledge Unveiled: Empower Yourself","publisher":{"@id":"https:\/\/learnscripting.org\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnscripting.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/learnscripting.org\/#organization","name":"Learn Scripting","url":"https:\/\/learnscripting.org\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnscripting.org\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/learnscripting.org\/wp-content\/uploads\/2022\/03\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1","contentUrl":"https:\/\/i0.wp.com\/learnscripting.org\/wp-content\/uploads\/2022\/03\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1","width":512,"height":512,"caption":"Learn Scripting"},"image":{"@id":"https:\/\/learnscripting.org\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/profile.php?id=100064270142636"]},{"@type":"Person","@id":"https:\/\/learnscripting.org\/#\/schema\/person\/71045e0c38b97ea7f76363d6effa320c","name":"Shakti Das","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","caption":"Shakti Das"},"sameAs":["https:\/\/learnscripting.org"],"url":"https:\/\/learnscripting.org\/author\/53kgl\/"}]}},"_links":{"self":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":0,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"wp:attachment":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}