{"id":2161,"date":"2020-03-17T17:34:06","date_gmt":"2020-03-17T17:34:06","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2161"},"modified":"2020-03-18T14:07:05","modified_gmt":"2020-03-18T14:07:05","slug":"introduction-to-polymorphism","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-polymorphism\/","title":{"rendered":"Introduction to Polymorphism"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Polymorphism means one name can have many forms.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To understand this, let us take an example, suppose you have a smartphone for communication purposes. The communication mode may vary. It can be anything that may be a call, a text message, a picture message, mail, etc. But the goal is the same in all communication modes, that is, communication. This is called <\/span><b>Polymorphism, <\/b><span style=\"font-weight: 400;\">where the purpose is the same but with different approaches. An example of polymorphism is Method Overriding.<\/span><\/p>\n<h2><b>Method Overriding<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Method Overriding is used to define a superclass method in a subclass.<\/span><\/p>\n<p><b>Some basic rules for Method Overriding<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The signature of the method, i.e., method name, parameter list, and return type should be the same<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If a method present in the superclass is defined as private, then the subclass can make it public but not vice versa.<\/span><\/li>\n<\/ul>\n<h2><b>What is Dynamic Polymorphism?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Dynamic Polymorphism is the process in which multiple methods are defined with the same name and signature in both superclass and subclass.\u00a0<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Example of Dynamic Polymorphism:<\/span><\/h3>\n<p style=\"padding-left: 60px;\"><span style=\"font-weight: 400;\">Doctor obj = new Surgeon();<br \/>\n<\/span><span style=\"font-weight: 400;\">Consider the statement<br \/>\n<\/span><span style=\"font-weight: 400;\">obj.treatPatient();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here the variable &#8220;obj&#8221; refers to the object of the parent class, but the object is pointing to the method of the child class.<\/span><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-2162 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_12-1.png\" alt=\"\" width=\"779\" height=\"460\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_12-1.png 779w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_12-1-300x177.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_12-1-768x454.png 768w\" sizes=\"(max-width: 779px) 100vw, 779px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">obj.treatPatient() will then execute treatPatient() method of the subclass named Surgeon<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If we use a subclass reference to call a method, then the method that is going to be invoked will be decided by the JVM.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, obj here is a reference object to Doctor, it will call the method of Surgeon, as obj here points to a Surgeon object<\/span><\/p>\n<p><span style=\"font-weight: 400;\">All these things are decided during run-time and hence called as\u00a0<\/span><b>dynamic\u00a0<\/b><i><span style=\"font-weight: 400;\">or\u00a0<\/span><\/i><b>run-time polymorphism.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">But what if the treatPatient method in the Surgeon class wants to access the functionality defined in Doctor class and then want to perform its specific functionality?<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">This can be achieved using the super keyword\u00a0to access methods of the superclass from the subclass.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">The treatPatient method in the Surgeon class will then be written as:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"font-weight: 400;\">treatPatient(){<br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0super.treatPatient();<br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\/\/add code specific to Surgeon<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The complete code will be :<\/span><\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-2163 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_13-1.png\" alt=\"\" width=\"673\" height=\"521\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_13-1.png 673w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_13-1-300x232.png 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/p>\n<p><img decoding=\"async\" class=\" wp-image-2164 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_14.png\" alt=\"\" width=\"708\" height=\"305\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_14.png 751w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_14-300x129.png 300w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/p>\n<h2><b>Difference between Static &amp; Dynamic Polymorphism<\/b><\/h2>\n<h2><\/h2>\n<table>\n<tbody>\n<tr>\n<td>\n<h4><b>Static Polymorphism<\/b><\/h4>\n<\/td>\n<td>\n<h4><b>Dynamic Polymorphism<\/b><\/h4>\n<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Related to method overloading.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Related to method overriding.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Errors get resolved at compile time only.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Errors get resolved at run time.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void sum (int a , int b);<br \/>\n<\/span><span style=\"font-weight: 400;\">void sum (float a, double b);<br \/>\n<\/span><span style=\"font-weight: 400;\">int sum (int a, int b); \/\/compiler gives error.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/\/reference of parent pointing to child object<br \/>\n<\/span><span style=\"font-weight: 400;\">Doctor obj = new Surgeon();<br \/>\n<\/span><span style=\"font-weight: 400;\">\/\/ method of child called<br \/>\n<\/span><span style=\"font-weight: 400;\">obj.treatPatient();<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism means one name can have many forms. To understand this, let us take an example, suppose you have a smartphone for communication purposes. The communication mode may vary. It can be anything that may be a call, a text message, a picture message, mail, etc. But the goal is the same in all communication [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2176,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[429,427,428],"class_list":["post-2161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-dynamic-polymorphism","tag-polymorphism","tag-static-polymorphism"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2161"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2161\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2176"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}