{"id":1245,"date":"2016-03-25T07:30:47","date_gmt":"2016-03-25T07:30:47","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1245"},"modified":"2016-03-25T07:30:47","modified_gmt":"2016-03-25T07:30:47","slug":"polymorphism","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/polymorphism","title":{"rendered":"Polymorphism In Java With Example"},"content":{"rendered":"<p>Polymorphism means one name and many duties. Polymorphism refers to the ability of one thing to take many(Poly) distinct forms(Morphism). It is the property by which same message is send to objects of different classes and the objects behave differently. It is a very powerful concept that allows the designing of amazingly flexible applications.<\/p>\n<p>Polymorphism can be defined as the ability to use the same for two or more related but technically different tasks. In other words, polymorphism can be defined as interface that can be used to perform related but different tasks.<\/p>\n<hr \/>\n<h4><strong>Types of Polymorphism<\/strong><\/h4>\n<p>There are two types of polymorphism:<\/p>\n<ol>\n<li>Compile time polymorphism<\/li>\n<li>Run time polymorphism<\/li>\n<\/ol>\n<p><span style=\"color: #008000;\"><strong><u>Compile Time Polymorphism<\/u><\/strong><strong>: <\/strong><\/span><\/p>\n<p>Whenever an object is bound with their functionality at compile time this is known as compile time polymorphism. At compile time, java knows which method to call by checking the method signatures. So this is called compile time polymorphism or static or early binding.<\/p>\n<p><strong>Implementation of Compile time polymorphism:<\/strong><\/p>\n<p>Compile time polymorphism is achieved through method\u00a0overloading.\u00a0<strong><a href=\"\/java\/method-overloading\">Method Overloading<\/a><\/strong> says you can have more than one function with a same name in one class having a different prototype. Function overloading is one of the way to achieve polymorphism but it depends on technology that which type of polymorphism we adopt. In java we achieve function overloading at run time.<\/p>\n<p><strong>Program Example of Compile time polymorphism:<\/strong><\/p>\n<p>Let us take an example to show the working of compile time polymorphism through function overloading.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Addition <\/strong>in which we declare three add() methods with different parameters:<\/p>\n<pre>class Addition\r\n{\r\n\r\n   public int add(int m,int n)\r\n   {\r\n\r\n      return m+n;\r\n   }\r\n\r\n   public int add(int m,int n,int o)\r\n   {\r\n\r\n     return m+n+o;\r\n   }\r\n\r\n   public int add(int m,double n)\r\n   {\r\n\r\n     return m+(int)n;\r\n   }\r\n\r\n}<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>PrintAdd <\/strong>in which we call the above class methods by creating an object:<\/p>\n<pre>class PrintAdd\r\n{\r\n\r\n  public static void main(String args[])\r\n  {\r\n\r\n     Addition a=new Addition();\r\n\r\n    System.out.println(a.add(2,3));\r\n\r\n    System.out.println(a.add(2,3,4));\r\n\r\n    System.out.println(a.add(2,3.4));\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>5\r\n9\r\n5\r\n<\/pre>\n<p><strong>Explanation of Example:<\/strong><\/p>\n<p>In above example there are three versions of add methods. The first method takes two parameters or arguments while the second one takes three parameters. In the third method there is change in type of parameters. The compiler looks at the method signature and decides which method to call for a particular method call at compile time.<\/p>\n<p><span style=\"color: #008000;\"><strong><u>Run Time Polymorphism<\/u><\/strong><\/span><strong><span style=\"color: #008000;\">:<\/span> <\/strong><\/p>\n<p>Whenever an object is bound with the functionality at run time, this is known as runtime polymorphism. You can have a method in subclass that overrides the method in its parent classes with the same name and parameters. Java virtual machine determines the proper method to call at the runtime, not at the compile time. It is also called dynamic or late binding.<\/p>\n<p><strong>Implementation of Run time polymorphism:<\/strong><\/p>\n<p>Run time or dynamic polymorphism is achieved(implemented) through method overriding. <strong><a href=\"\/java\/method-overriding\">Method overriding<\/a><\/strong> says child class has the same method as declared in the parent class. It means if child class provides the specific implementation of the method that has been provided by one of its parent class then it is known as method overriding.<\/p>\n<p><strong>Program Example of Run time polymorphism:<\/strong><\/p>\n<p>Let us take an example to show the working of run time polymorphism through method overriding.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Vehicle <\/strong>in which we declare a move() method:<\/p>\n<pre>class Vehicle\r\n{\r\n\r\n  public void move()\r\n  {\r\n\r\n    System.out.println(\"vehicles can move\");\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>Bike <\/strong>which extends the class <strong>Vehicle:<\/strong><\/p>\n<pre>class Bike extends Vehicle\r\n{\r\n\r\n  public void move()\r\n  {\r\n\r\n    System.out.println(\"bike can move and accelerate too\");\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Step 3: <\/strong>Third we create a class <strong>TestVehicle <\/strong>in which we create an object and call the above class method:<\/p>\n<pre>class TestVehicle\r\n{\r\n\r\n  public static void main(String args[])\r\n  {\r\n\r\n    Vehicle v=new Bike();\r\n\r\n    v.move();\r\n\r\n    Vehicle v1=new Vehicle();\r\n\r\n    v1.move();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Bike can move and accelerate too\r\nVehicles can move<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>In above example, it should be noted that in the first call to <strong>move()<\/strong>,the reference type is <strong>Vehicle<\/strong> and the object is being referenced is <strong>Bike<\/strong>. So when a call to <strong>move()<\/strong> is made, java waits until runtime to check which object is being pointed to by reference. In this case,the object is of class <strong>Bike<\/strong>. So the <strong>move()<\/strong> method of <strong>Bike<\/strong> class will be called. In the second call to <strong>move()<\/strong>,the object is of class <strong>Vehicle<\/strong>. So the <strong>move()<\/strong> method of <strong>Vehicle<\/strong> will be called.<\/p>\n<hr \/>\n<h4><strong>Difference Between Compile Time and Run Time Polymorphism<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong><u>Compile time polymorphism:<\/u><\/strong><\/span><\/p>\n<ol>\n<li>In compile time polymorphism, call is resolved by the compiler.<\/li>\n<li>It is also known as static or early binding.<\/li>\n<li>It is achieved by function overloading and operator overloading.<\/li>\n<li>It provides fast execution because known early at compile time.<\/li>\n<li>Compile time polymorphism is less flexible as all things execute at compile time.<\/li>\n<\/ol>\n<p><span style=\"color: #008000;\"><strong>\u00a0<u>Run time polymorphism :<\/u><\/strong><\/span><\/p>\n<ol>\n<li>In run time polymorphism, call is not resolved by the compiler.<\/li>\n<li>It is also known as dynamic binding or late binding.<\/li>\n<li>It is achieved by virtual functions.<\/li>\n<li>It provides slow execution as compare to early binding because it is known as runtime.<\/li>\n<li>Run time polymorphism is more flexible as all things execute at run time.<\/li>\n<\/ol>\n<hr \/>\n<h4><strong>Importance of Polymorphism<\/strong><\/h4>\n<ul>\n<li>It reduces the complexity of the object.<\/li>\n<li>Through polymorphism complete implementation can be replaced by using same method signatures.<\/li>\n<li>It reduces the volume of work in terms of handling various objects.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Polymorphism Important Points To Remember<\/strong><\/h4>\n<ul>\n<li>Java does not support operator overloading directly.<\/li>\n<li>Java does not support compile time polymorphism.<\/li>\n<li>Access specifiers and access modifiers do not play any role in case of function overloading.<\/li>\n<li>If you want to keep number of arguments in each function same and still you want to overload them, then change the data type of their arguments.<\/li>\n<li>If a function is returning the value then it is not mandatory to catch the return value while calling it.<\/li>\n<li>Return type of a function does not play any role in case of function overloading.<\/li>\n<li>Function overloading can only be achieved by changing only in arguments.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism means one name and many duties. Polymorphism refers to the ability of one thing to take many(Poly) distinct forms(Morphism). It is the property by which same message is send to objects of different classes and the objects behave differently. It is a very powerful concept that allows the designing of amazingly flexible applications. Polymorphism &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/polymorphism\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Polymorphism In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-1245","page","type-page","status-publish","hentry"],"psp_head":"<title>Polymorphism In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Polymorphism tutorial with example in JAVA. Learn compile and run time polymorphism in detail. Also find difference between the two types of polymorphism.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/polymorphism\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1245"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1245\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}