{"id":210,"date":"2022-10-23T07:44:51","date_gmt":"2022-10-23T07:44:51","guid":{"rendered":"https:\/\/valerio.nu\/?p=210"},"modified":"2022-10-23T07:44:52","modified_gmt":"2022-10-23T07:44:52","slug":"runtime-polymorphism-in-java","status":"publish","type":"post","link":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/","title":{"rendered":"Runtime Polymorphism in Java"},"content":{"rendered":"\n<p>In the object-oriented programming world, polymorphism is an interesting mechanism that allows methods of a class to take multiple forms. While compile-time polymorphism largely deals with method overloading, runtime polymorphism enhances the simplicity of inheritance, a pillar of the object-oriented programming language, by method overriding.<\/p>\n\n\n\n<p>In Java, a child class has the flexibility to override the parent\u2019s behavior without impacting the parent class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">So, What is Runtime Polymorphism?<\/h2>\n\n\n\n<p>It is a technique that allows the JVM to invoke an overridden child method dynamically using the parent reference during runtime. Hence, this is known as dynamic polymorphism.<\/p>\n\n\n\n<p>Since the methods are dynamically resolved, this concept is also known as Dynamic Method Dispatch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Does it Work?<\/h2>\n\n\n\n<p>A child class overrides a method of the parent class. The overridden method can be invoked using a parent class reference. This is known as upcasting. The compiler will not know as to which method to be called as the method is present in both the parent and the child classes. It is resolved during runtime by the JVM based on the object the parent is referring to.<\/p>\n\n\n\n<p>Runtime polymorphism can be achieved for single and multilevel hierarchy classes. However, if the child classes do not override the method of the parent class, then the parent class method is called during execution.<\/p>\n\n\n\n<p>Let\u2019s simplify it with an example.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"553\" src=\"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-1024x553.png\" alt=\"\" class=\"wp-image-211\" srcset=\"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-1024x553.png 1024w, https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-300x162.png 300w, https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-768x415.png 768w, https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-1536x830.png 1536w, https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly.png 1868w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>An example of runtime polymorphism<\/figcaption><\/figure>\n\n\n\n<p>Class Bank contains a method getInterestRate() that displays the interest rate for that bank.<br>Class DBCS extends Bank, and SCB extends DBCS. Both the DBCS and the SCB banks have overridden the method getInterestRate() from the parent class.<\/p>\n\n\n\n<p>Let\u2019s create an object of class SCB using the Bank reference.<br>When the getInterestRate() method is called, the JVM invokes the SCB method call during runtime.<\/p>\n\n\n\n<p>Similarly, the JVM calls the method getBankName() from DBCS class as this method is not overridden in the SCB class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Limitations of Runtime Polymorphism<\/h2>\n\n\n\n<p>Although Dynamic polymorphism is useful in many scenarios, there are a few limitations too:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Runtime polymorphism works only for the methods of a class and not for the variables<\/li><li>It can be achieved only for the overridden methods<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the object-oriented programming world, polymorphism is an interesting mechanism that allows methods of a class to take multiple forms. While compile-time polymorphism largely deals [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Runtime Polymorphism in Java - Andy&#039;s blog \u2935\ufe0f<\/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:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Runtime Polymorphism in Java - Andy&#039;s blog \u2935\ufe0f\" \/>\n<meta property=\"og:description\" content=\"In the object-oriented programming world, polymorphism is an interesting mechanism that allows methods of a class to take multiple forms. While compile-time polymorphism largely deals [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Andy&#039;s blog \u2935\ufe0f\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-23T07:44:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-23T07:44:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-1024x553.png\" \/>\n<meta name=\"author\" content=\"andy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"andy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/\"},\"author\":{\"name\":\"andy\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"headline\":\"Runtime Polymorphism in Java\",\"datePublished\":\"2022-10-23T07:44:51+00:00\",\"dateModified\":\"2022-10-23T07:44:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/\"},\"wordCount\":361,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/poly-1024x553.png\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/\",\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/\",\"name\":\"Runtime Polymorphism in Java - Andy&#039;s blog \u2935\ufe0f\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/poly-1024x553.png\",\"datePublished\":\"2022-10-23T07:44:51+00:00\",\"dateModified\":\"2022-10-23T07:44:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/poly.png\",\"contentUrl\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/poly.png\",\"width\":1868,\"height\":1009},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/10\\\/23\\\/runtime-polymorphism-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/valerio.nu\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Runtime Polymorphism in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\",\"url\":\"https:\\\/\\\/valerio.nu\\\/\",\"name\":\"Andy&#039;s blog \u2935\ufe0f\",\"description\":\"Abandon all hope, ye who enter here\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/valerio.nu\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\",\"name\":\"andy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"caption\":\"andy\"},\"sameAs\":[\"https:\\\/\\\/valerio.nu\"],\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/author\\\/giuseppe\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Runtime Polymorphism in Java - Andy&#039;s blog \u2935\ufe0f","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:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Runtime Polymorphism in Java - Andy&#039;s blog \u2935\ufe0f","og_description":"In the object-oriented programming world, polymorphism is an interesting mechanism that allows methods of a class to take multiple forms. While compile-time polymorphism largely deals [&hellip;]","og_url":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/","og_site_name":"Andy&#039;s blog \u2935\ufe0f","article_published_time":"2022-10-23T07:44:51+00:00","article_modified_time":"2022-10-23T07:44:52+00:00","og_image":[{"url":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-1024x553.png","type":"","width":"","height":""}],"author":"andy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"andy","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#article","isPartOf":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/"},"author":{"name":"andy","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"headline":"Runtime Polymorphism in Java","datePublished":"2022-10-23T07:44:51+00:00","dateModified":"2022-10-23T07:44:52+00:00","mainEntityOfPage":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/"},"wordCount":361,"commentCount":0,"image":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-1024x553.png","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/","url":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/","name":"Runtime Polymorphism in Java - Andy&#039;s blog \u2935\ufe0f","isPartOf":{"@id":"https:\/\/valerio.nu\/#website"},"primaryImageOfPage":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#primaryimage"},"image":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly-1024x553.png","datePublished":"2022-10-23T07:44:51+00:00","dateModified":"2022-10-23T07:44:52+00:00","author":{"@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"breadcrumb":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#primaryimage","url":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly.png","contentUrl":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/poly.png","width":1868,"height":1009},{"@type":"BreadcrumbList","@id":"https:\/\/valerio.nu\/index.php\/2022\/10\/23\/runtime-polymorphism-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/valerio.nu\/"},{"@type":"ListItem","position":2,"name":"Runtime Polymorphism in Java"}]},{"@type":"WebSite","@id":"https:\/\/valerio.nu\/#website","url":"https:\/\/valerio.nu\/","name":"Andy&#039;s blog \u2935\ufe0f","description":"Abandon all hope, ye who enter here","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/valerio.nu\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128","name":"andy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","caption":"andy"},"sameAs":["https:\/\/valerio.nu"],"url":"https:\/\/valerio.nu\/index.php\/author\/giuseppe\/"}]}},"_links":{"self":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":1,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}