{"id":1212,"date":"2022-02-25T16:53:53","date_gmt":"2022-02-25T16:53:53","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1212"},"modified":"2022-02-25T16:53:53","modified_gmt":"2022-02-25T16:53:53","slug":"java-polymorphism-2","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-polymorphism-2\/","title":{"rendered":"Java Polymorphism"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java polymorphism and its implementation with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">Polymorphism is an important concept of object-oriented programming. It simply means more than one form.<\/p>\n\n\n\n<p>That is, the same entity (method or operator or object) can perform different operations in different scenarios.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: Java Polymorphism<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Polygon {\n\n  \/\/ method to render a shape\n  public void render() {\n    System.out.println(\"Rendering Polygon...\");\n  }\n}\n\nclass Square extends Polygon {\n\n  \/\/ renders Square\n  public void render() {\n    System.out.println(\"Rendering Square...\");\n  }\n}\n\nclass Circle extends Polygon {\n\n  \/\/ renders circle\n  public void render() {\n    System.out.println(\"Rendering Circle...\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    \n    \/\/ create an object of Square\n    Square s1 = new Square();\n    s1.render();\n\n    \/\/ create an object of Circle\n    Circle c1 = new Circle();\n    c1.render();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Rendering Square...\nRendering Circle...<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a superclass:&nbsp;<var>Polygon<\/var>&nbsp;and two subclasses:&nbsp;<var>Square<\/var>&nbsp;and&nbsp;<var>Circle<\/var>. Notice the use of the&nbsp;<code>render()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>The main purpose of the&nbsp;<code>render()<\/code>&nbsp;method is to render the shape. However, the process of rendering a square is different than the process of rendering a circle.<\/p>\n\n\n\n<p>Hence, the&nbsp;<code>render()<\/code>&nbsp;method behaves differently in different classes. Or, we can say&nbsp;<code>render()<\/code>&nbsp;is polymorphic.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"why\">Why Polymorphism?<\/h3>\n\n\n\n<p>Polymorphism allows us to create consistent code. In the previous example, we can also create different methods:&nbsp;<code>renderSquare()<\/code>&nbsp;and&nbsp;<code>renderCircle()<\/code>&nbsp;to render&nbsp;<var>Square<\/var>&nbsp;and&nbsp;<var>Circle<\/var>, respectively.<\/p>\n\n\n\n<p>This will work perfectly. However, for every shape, we need to create different methods. It will make our code inconsistent.<\/p>\n\n\n\n<p>To solve this, polymorphism in Java allows us to create a single method&nbsp;<code>render()<\/code>&nbsp;that will behave differently for different shapes.<\/p>\n\n\n\n<p><strong>Note<\/strong>: The&nbsp;<code>print()<\/code>&nbsp;method is also an example of polymorphism. It is used to print values of different types like&nbsp;<code>char<\/code>,&nbsp;<code>int<\/code>,&nbsp;<code>string<\/code>, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>We can achieve polymorphism in Java using the following ways:<\/p>\n\n\n\n<ol><li>Method Overriding<\/li><li>Method Overloading<\/li><li>Operator Overloading<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"overriding\">Java Method Overriding<\/h2>\n\n\n\n<p>During\u00a0inheritance in Java, if the same method is present in both the superclass and the subclass. Then, the method in the subclass overrides the same method in the superclass. This is called method overriding.<\/p>\n\n\n\n<p>In this case, the same method will perform one operation in the superclass and another operation in the subclass. For example,<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Polymorphism using method overriding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Language {\n  public void displayInfo() {\n    System.out.println(\"Common English Language\");\n  }\n}\n\nclass Java extends Language {\n  @Override\n  public void displayInfo() {\n    System.out.println(\"Java Programming Language\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of Java class\n    Java j1 = new Java();\n    j1.displayInfo();\n\n    \/\/ create an object of Language class\n    Language l1 = new Language();\n    l1.displayInfo();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Java Programming Language\nCommon English Language<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a superclass named&nbsp;<var>Language<\/var>&nbsp;and a subclass named&nbsp;<var>Java<\/var>. Here, the method&nbsp;<code>displayInfo()<\/code>&nbsp;is present in both&nbsp;<var>Language<\/var>&nbsp;and&nbsp;<var>Java<\/var>.<\/p>\n\n\n\n<p>The use of&nbsp;<code>displayInfo()<\/code>&nbsp;is to print the information. However, it is printing different information in&nbsp;<var>Language<\/var>&nbsp;and&nbsp;<var>Java<\/var>.<\/p>\n\n\n\n<p>Based on the object used to call the method, the corresponding information is printed.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-polymorphism-implementation.png\" alt=\"displayInfo() method prints Common English Language when called using l1 object and when using j1 object, it prints Java Programming Language\" title=\"Java Polymorphism Implementation\"\/><figcaption>Working of Java Polymorphism<\/figcaption><\/figure>\n\n\n\n<p><strong>Note<\/strong>: The method that is called is determined during the execution of the program. Hence, method overriding is a&nbsp;<strong>run-time polymorphism<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-overloading\">2. Java Method Overloading<\/h2>\n\n\n\n<p>In a Java class, we can create methods with the same name if they differ in parameters. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func() { ... }\nvoid func(int a) { ... }\nfloat func(double a) { ... }\nfloat func(int a, float b) { ... }<\/code><\/pre>\n\n\n\n<p>This is known as method overloading in Java. Here, the same method will perform different operations based on the parameter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Polymorphism using method overloading<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Pattern {\n\n  \/\/ method without parameter\n  public void display() {\n    for (int i = 0; i &lt; 10; i++) {\n      System.out.print(\"*\");\n    }\n  }\n\n  \/\/ method with single parameter\n  public void display(char symbol) {\n    for (int i = 0; i &lt; 10; i++) {\n      System.out.print(symbol);\n    }\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Pattern d1 = new Pattern();\n\n    \/\/ call method without any argument\n    d1.display();\n    System.out.println(\"\\n\");\n\n    \/\/ call method with a single argument\n    d1.display('#');\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>**********\n\n##########<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a class named&nbsp;<var>Pattern<\/var>. The class contains a method named&nbsp;<code>display()<\/code>&nbsp;that is overloaded.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ method with no arguments\ndisplay() {...}\n\n\/\/ method with a single char type argument\ndisplay(char symbol) {...}<\/code><\/pre>\n\n\n\n<p>Here, the main function of&nbsp;<code>display()<\/code>&nbsp;is to print the pattern. However, based on the arguments passed, the method is performing different operations:<\/p>\n\n\n\n<ul><li>prints a pattern of&nbsp;<code>*<\/code>, if no argument is passed or<\/li><li>prints pattern of the parameter, if a single&nbsp;<code>char<\/code>&nbsp;type argument is passed.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: The method that is called is determined by the compiler. Hence, it is also known as compile-time polymorphism.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"operator-overloading\">3. Java Operator Overloading<\/h2>\n\n\n\n<p>Some operators in Java behave differently with different operands. For example,<\/p>\n\n\n\n<ul><li><code>+<\/code>&nbsp;operator is overloaded to perform numeric addition as well as string concatenation, and<\/li><li>operators like&nbsp;<code>&amp;<\/code>,&nbsp;<code>|<\/code>, and&nbsp;<code>!<\/code>&nbsp;are overloaded for logical and bitwise operations.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s see how we can achieve polymorphism using operator overloading.<\/p>\n\n\n\n<p>The&nbsp;<code>+<\/code>&nbsp;operator is used to add two entities. However, in Java, the&nbsp;<code>+<\/code>&nbsp;operator performs two operations.<\/p>\n\n\n\n<p>1. When&nbsp;<code>+<\/code>&nbsp;is used with numbers (integers and floating-point numbers), it performs mathematical addition. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 5;\nint b = 6;\n\n\/\/ + with numbers\nint sum = a + b;  \/\/ Output = 11<\/code><\/pre>\n\n\n\n<p>2. When we use the&nbsp;<code>+<\/code>&nbsp;operator with strings, it will perform string concatenation (join two strings). For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String first = \"Java \";\nString second = \"Programming\";\n\n\/\/ + with strings\nname = first + second;  \/\/ Output = Java Programming<\/code><\/pre>\n\n\n\n<p>Here, we can see that the&nbsp;<code>+<\/code>&nbsp;operator is overloaded in Java to perform two operations:&nbsp;<strong>addition<\/strong>&nbsp;and&nbsp;<strong>concatenation<\/strong>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: In languages like C++, we can define operators to work differently for different operands. However, Java doesn&#8217;t support user-defined operator overloading.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"polymorphic-variable\">Polymorphic Variables<\/h2>\n\n\n\n<p>A variable is called polymorphic if it refers to different values under different conditions.<\/p>\n\n\n\n<p>Object variables (instance variables) represent the behavior of polymorphic variables in Java. It is because object variables of a class can refer to objects of its class as well as objects of its subclasses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Polymorphic Variables<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProgrammingLanguage {\n  public void display() {\n    System.out.println(\"I am Programming Language.\");\n  }\n}\n\nclass Java extends ProgrammingLanguage {\n  @Override\n  public void display() {\n    System.out.println(\"I am Object-Oriented Programming Language.\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ declare an object variable\n    ProgrammingLanguage pl;\n\n    \/\/ create object of ProgrammingLanguage\n    pl = new ProgrammingLanguage();\n    pl.display();\n\n    \/\/ create object of Java class\n    pl = new Java();\n    pl.display();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I am Programming Language.\nI am Object-Oriented Programming Language.<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an object variable&nbsp;<var>pl<\/var>&nbsp;of the&nbsp;<var>ProgrammingLanguage<\/var>&nbsp;class. Here,&nbsp;<var>pl<\/var>&nbsp;is a polymorphic variable. This is because,<\/p>\n\n\n\n<ul><li>In statement&nbsp;<code>pl = new ProgrammingLanguage()<\/code>,&nbsp;<var>pl<\/var>&nbsp;refer to the object of the&nbsp;<var>ProgrammingLanguage<\/var>&nbsp;class.<\/li><li>And, in statement&nbsp;<code>pl = new Java()<\/code>,&nbsp;<var>pl<\/var>&nbsp;refer to the object of the&nbsp;<var>Java<\/var>&nbsp;class.<\/li><\/ul>\n\n\n\n<p>This is an example of upcasting in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java polymorphism and its implementation with the help of examples. Polymorphism is an important concept of object-oriented programming. It simply means more than one form. That is, the same entity (method or operator or object) can perform different operations in different scenarios. Example: Java Polymorphism Output In the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1212"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1212"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1212\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}