{"id":2376,"date":"2022-04-04T06:31:05","date_gmt":"2022-04-04T06:31:05","guid":{"rendered":"https:\/\/blog.embold.io\/?p=2376"},"modified":"2022-04-04T06:55:29","modified_gmt":"2022-04-04T06:55:29","slug":"how-does-polymorphism-work-in-java","status":"publish","type":"post","link":"https:\/\/blog.embold.io\/ru\/how-does-polymorphism-work-in-java\/","title":{"rendered":"How Does Polymorphism Work In Java?"},"content":{"rendered":"\n<p>Polymorphism in java can be understood as\nan ability of an object to take many forms. In a simple language, With the help\nof Polymorphism in Java we have liberty to perform multiple actions in a\nvarious ways and any java object&nbsp; that\ncan process more than one IS-A test is known as Polymorphic.<\/p>\n\n\n\n<p>Let\u2019s take an example to understand polymorphism in a simple manner, consider a superclass called Furntiure, which has a method named FurnitureItems (). Chair, Bed, Table, and Dinning table are subclasses of Furniture and each has its own uses (the chair for sitting, the bed for sleeping, and so on):<\/p>\n\n\n\n<h2>Polymorphism\u201d: Real life Example<\/h2>\n\n\n\n<p>A person can have a variety of\nrelationships with different people. A Man can be a father, a son, a brother,\nand a friend all at once, i.e. he can act in different ways in different\nsituations.<\/p>\n\n\n\n<p>Same as human organs. All organs are related to human body but all have different functions. The heart is in charge of blood flow, the lungs of breathing, the brain of cognition, and the kidneys of excretion. Hence, we have a standard method function that behaves differently depending on the body organ.<\/p>\n\n\n\n<h2>Polymorphism in Java<\/h2>\n\n\n\n<p>When there is inheritance, or when there are many classes that are related to each other, polymorphism occurs. Object-Based Languages, not Object-Oriented Languages, are those that do not support polymorphism. Java is an Object-Oriented Language because it supports polymorphism. When there is inheritance, or when there are many classes that are related to each other, polymorphism occurs.<\/p>\n\n\n\n<h2><strong>Polymorphism in Java Example<\/strong><\/h2>\n\n\n\n<p>A superclass named \u201cFurniture\u201d has a method \u201cItems()\u201d. Subclasses of \u201cfurniture\u201d can be \u201cSleeping, \u201csitting\u201d, \u201cEating\u201d, etc. Each subclass has its way of uses. Using Inheritance and Polymorphism means, the subclasses can have \u201cuses()\u201d method to find the uses\u2019s formula for that furniture.<\/p>\n\n\n\n<p>class Furniture {<\/p>\n\n\n\n<p>&nbsp; public void furnitureItems() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; System.out.println(&#8220;Use\nof Furntiure&#8221;);<\/p>\n\n\n\n<p>&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class<code> <\/code>Chair<code> <\/code>extends<code> <\/code>Furniture<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>public<code> <\/code>void<code> <\/code>furnitureItems()<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>System.<code>out<\/code>.println(\"The Chair: For Sitting\");<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>}<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">class<code> <\/code>Dinning table<code> <\/code>extends<code> <\/code>Furniture<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>public<code> <\/code>void<code> <\/code>FurnitureItems()<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>System.<code>out<\/code>.println(\"The Dinning table: for eating\");<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>}<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Now we can create Chair and Bed objects and call the furnitureItems() method on both of them<\/strong>.<\/pre>\n\n\n\n<h3><strong>Example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">class<code> <\/code>Furniture<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>public<code> <\/code>void<code> <\/code>FurnitureItems()<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>System.<code>out<\/code>.println(\"Uses of Furniture\");<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>}<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">class<code> <\/code>Chair<code> <\/code>extends<code> <\/code>Furniture<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>public<code> <\/code>void<code> <\/code>FurnitureItems()<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>System.<code>out<\/code>.println(\"The chair: For Sitting\");<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>}<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">class<code> <\/code>Dinning Table extends<code> <\/code>Furniture{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>public<code> <\/code>void<code> <\/code>FurnitureItems()<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>System.<code>out<\/code>.println(\"The Dinning Table: For Eating\");<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>}<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<code><\/code><\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u00a0<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">class<code> <\/code>Main<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>public<code> <\/code>static<code> <\/code>void<code> <\/code>main(String[]<code> args<\/code>)<code> <\/code>{<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>Furniture<code> my<\/code>Furniture<code> <\/code>=<code> <\/code>new<code> <\/code>Furniture();<code>&nbsp; <\/code>\/\/ Create a Furniture object<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp; <\/code>Furniture <code>myChair <\/code>=<code> <\/code>new<code> <\/code>Chair();<code>&nbsp; <\/code>\/\/ Create a Chair object<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp; <\/code>Furniture <code>myDinningTable<\/code>=<code> <\/code>new<code> <\/code>DinningTable();<code>&nbsp; <\/code>\/\/ Create a DinningTable object<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; myFurniture<\/code>. FurniturItems();<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; myChair<\/code>. FurnitureItems();<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; myDinningTable<\/code>. FurnitureItems();<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp; <\/code>}<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<\/pre>\n\n\n\n<h2>Types of\nPolymorphism<\/h2>\n\n\n\n<p>Polymorphism can be done in Java using two alternative\nmethods:<\/p>\n\n\n\n<ol><li>Method Overloading<\/li><li>Method Overriding<\/li><\/ol>\n\n\n\n<h3><strong><span style=\"text-decoration: underline\">Method Of Overloading<\/span><\/strong><\/h3>\n\n\n\n<p> The process of creating multiple methods with the same name in the same class, each of which performs a different function, is known as method overloading.  <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"525\" height=\"401\" src=\"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/MicrosoftTeams-image.png\" alt=\"Example of method of overloading\" class=\"wp-image-2381\" \/><\/figure>\n\n\n\n<h3><strong><span style=\"text-decoration: underline\">Method Of Overriding<\/span><\/strong><\/h3>\n\n\n\n<p>When a subclass or a child class has the same method as the parent class,\nthis is known as method overriding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism in java can be understood as an ability of an object to take many forms. In a simple language, With the help of Polymorphism in Java we have liberty&hellip;<\/p>\n","protected":false},"author":24,"featured_media":2378,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[108,301],"better_featured_image":{"id":2378,"alt_text":"Polymorphism work in java","caption":"","description":"","media_type":"image","media_details":{"width":3000,"height":2000,"file":"2022\/04\/jp.jpg","sizes":{"thumbnail":{"file":"jp-150x150.jpg","width":150,"height":150,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-150x150.jpg"},"medium_large":{"file":"jp-768x512.jpg","width":768,"height":512,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-768x512.jpg"},"penci-full-thumb":{"file":"jp-1170x780.jpg","width":1170,"height":780,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-1170x780.jpg"},"penci-slider-thumb":{"file":"jp-1170x663.jpg","width":1170,"height":663,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-1170x663.jpg"},"penci-magazine-slider":{"file":"jp-780x516.jpg","width":780,"height":516,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-780x516.jpg"},"penci-slider-full-thumb":{"file":"jp-1920x800.jpg","width":1920,"height":800,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-1920x800.jpg"},"penci-single-full":{"file":"jp-1920x1280.jpg","width":1920,"height":1280,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-1920x1280.jpg"},"penci-thumb":{"file":"jp-585x390.jpg","width":585,"height":390,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-585x390.jpg"},"penci-masonry-thumb":{"file":"jp-585x390.jpg","width":585,"height":390,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-585x390.jpg"},"penci-thumb-square":{"file":"jp-585x585.jpg","width":585,"height":585,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-585x585.jpg"},"penci-thumb-vertical":{"file":"jp-480x650.jpg","width":480,"height":650,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-480x650.jpg"},"penci-thumb-small":{"file":"jp-263x175.jpg","width":263,"height":175,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-263x175.jpg"},"jr_insta_square":{"file":"jp-640x640.jpg","width":640,"height":640,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp-640x640.jpg"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"post":2376,"source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2022\/04\/jp.jpg"},"translation":{"provider":"WPGlobus","version":"2.10.8","language":"ru","enabled_languages":["en","es","de","fr","ru"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"es":{"title":false,"content":false,"excerpt":false},"de":{"title":false,"content":false,"excerpt":false},"fr":{"title":false,"content":false,"excerpt":false},"ru":{"title":false,"content":false,"excerpt":false}}},"_links":{"self":[{"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/posts\/2376"}],"collection":[{"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/comments?post=2376"}],"version-history":[{"count":5,"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/posts\/2376\/revisions"}],"predecessor-version":[{"id":2384,"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/posts\/2376\/revisions\/2384"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/media\/2378"}],"wp:attachment":[{"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/media?parent=2376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/categories?post=2376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embold.io\/ru\/wp-json\/wp\/v2\/tags?post=2376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}