{"id":533,"date":"2016-01-09T14:07:59","date_gmt":"2016-01-09T14:07:59","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=533"},"modified":"2016-02-25T06:24:58","modified_gmt":"2016-02-25T06:24:58","slug":"method-overriding","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/method-overriding","title":{"rendered":"Method Overriding Tutorial With Examples In JAVA"},"content":{"rendered":"<p>Overriding means to\u00a0extend\u00a0or to\u00a0pass\u00a0over something, especially to\u00a0overlap the previous described functionality. So Method Overriding means to re-write the previous described method again of Parent Class in Sub class with different functionality.<\/p>\n<p>In Method Overriding, we overrides the method of Super Class in Sub Class. The method overriding is also called Run time Polymorphism or Dynamic Binding.<\/p>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Method Overriding Explanation With Example:<\/strong><\/span><\/h4>\n<p>Before discussing <strong>Method Overriding<\/strong>, let us revise our concept of<strong> <a href=\"\/java\/inheritance\">Inheritance<\/a><\/strong>. Inheritance is a phenomena in which one class extends the properties( i.e. methods and fields) of another class. It makes our programming much easier as previously defined code is reused, and we can add our own functionality.<\/p>\n<p>The class which extends the properties of another class is called Sub-Class or Child-Class or Derived-Class. And the class whose methods and fields or any other property is inherited is called Super-Class or Base-Class or Parent-Class.<\/p>\n<p><span style=\"color: #008000;\"><strong>Why Method Overriding?<\/strong><\/span> Now let us understand the problem that we face without method overriding.<\/p>\n<p>In the following program we have Animal and Horse classes. In <strong>Animal Class<\/strong> we have defined a method which <strong>prints \u201cAnimal is running\u201d.<\/strong>\u00a0In <strong>Horse class<\/strong> we have used the already defined <strong>method run()<\/strong>\u00a0of Animal Class as shown:<\/p>\n<p>Below is code of Animal class:<\/p>\n<pre>class Animal {\r\n\r\nvoid run(){\r\nSystem.out.println(\"Animal is running\");\r\n}\r\n\r\n}<\/pre>\n<p>Below is the code of Horse class which extend Animal class<\/p>\n<pre>public class Horse extends Animal\r\n{\r\npublic static void main(String args[]){\r\nHorse horse = new Horse(); \/\/Horse class object is created\r\nhorse.run();\r\n}\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Animal is running\r\n<\/pre>\n<p><em><span style=\"color: #008000;\"><strong>Problem &amp; Solution:<\/strong><\/span> But the problem here is if we have to give specific implementation to Horse Class object, that \u201cHorse is running\u201d then how can we do that? The answer is Method overriding! So to give specific implementation to parent class method in sub-class we need Method Overriding.<br \/>\n<\/em><\/p>\n<p><span style=\"color: #008000;\"><strong>So let\u2019s come to Method Overriding:<\/strong><\/span><\/p>\n<ul>\n<li>In this process when a method defined in super class the same method is defined again in sub class<\/li>\n<li>In sub class new functionality is defined for that method which overlap the functionality defined in Parent Class. So here method is overridden.<\/li>\n<li>In other words we can say defining and giving a new functionality to the same method in sub class which is already present in parent class is called Method Overriding.<\/li>\n<\/ul>\n<p><strong>Lets discuss the same example again using method overriding:<\/strong><\/p>\n<pre>class Animal {\r\n\r\nvoid run()\r\n{\r\nSystem.out.println(\"Animal is running\");\r\n}\r\n\r\n}<\/pre>\n<p>Below is the Horse class code which override run method of Animal class after extending it:<\/p>\n<pre>public class Horse extends Animal\r\n\r\n{\r\n\/\/run() Method is overridden in Sub-Class\r\nvoid run(){\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\nSystem.out.println(\"Horse is running\");\r\n}\r\n\r\npublic static void main(String args[]){\r\nHorse horse = new Horse();\r\nhorse.run();\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Horse is running\r\n<\/pre>\n<p>Here we see method run() is defined again in sub class Horse or in other words method run() is overlapped in the child class, giving it a different functionality. So now we have given specific implementation to the child class run() method.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>Method Overriding is also called as Run time Polymorphism or Dynamic Binding. This is due to the fact that the compiler doesn&#8217;t necessarily know what type of object is being passed at compile-time.<\/p>\n<hr \/>\n<h4><strong>Writing @Override Annotation Is Optional<\/strong><\/h4>\n<p>When you override any method, it is optional but recommended in JAVA to write @Override just above the method which you are overriding. This helps JAVA compiler to know that we want to override a method here which is already present in Parent class. But in-case if it is not present the compiler will give error letting us know that there is no method like that in Parent class.<\/p>\n<p><strong>Below is the Syntax of Method Overriding using @Override<\/strong><\/p>\n<pre>class Parent{\r\nmethod(){\r\n\/\/Add Functionality here\r\n}\r\n}\r\n\r\nclass Subclass extends Parent{\r\n@Override\r\nmethod(){\r\n\/\/New Functionality here for method\r\n}\r\n}<\/pre>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Real life example\u00a0of Method Overriding :<\/strong><\/span><\/h4>\n<p>Lets take a real life situation where we will need Method Overriding to code in JAVA.<\/p>\n<p>Consider a case, where Hospital provides no. of patients admitted in it. But number of patients varies with the different hospitals, For example Health India hospital has 1657 patients, IVY hospital has 2965 patients\u00a0 and Apollo Hospital has 1631 patients.<\/p>\n<p>Below is code of Hospital parent class which has one method getNumberOfPatients() and sub class HealthIndia, IVY and Apolo class which extends parent class &amp; override its method.<\/p>\n<pre>class Hospital{\r\n\r\n\tint getNumberOfPatients()\r\n\r\n\t{\r\n\r\n\t\treturn 0;\r\n\r\n\t}\r\n\r\n}\r\n\r\nclass HealthIndia extends Hospital{\r\n\r\n\t@Override\r\n\tint getNumberOfPatients()\r\n\r\n\t{\r\n\r\n\t\treturn 1657 ;\r\n\r\n\t}\r\n\r\n}\r\n\r\nclass IVY extends Hospital{\r\n\t@Override\r\n\tint getNumberOfPatients()\r\n\r\n\t{\r\n\r\n\t\treturn 2965 ;\r\n\r\n\t}\r\n\r\n}\r\n\r\nclass Apollo extends Hospital{\r\n\t@Override\r\n\tint getNumberOfPatients()\r\n\r\n\t{\r\n\r\n\t\treturn 1631 ;\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p>Below is the code of MethodOverriding class which is the main class. Here we will create object for each sub class and run getNumberOfPatients() method on those objects to get patient details of each Hospital.<\/p>\n<pre>public class MethodOverriding{\r\n\r\n\tpublic static void main(String args[]){\r\n\r\n\t\tHealthIndia healthIndia=new HealthIndia();\r\n\r\n\t\tIVY ivy=new IVY();\r\n\r\n\t\tApollo apollo=new Apollo();\r\n\r\n\t\tint healthIndiaPatients= healthIndia.getNumberOfPatients();\r\n\r\n\t\tint iVYPatients= ivy.getNumberOfPatients();\r\n\r\n\t\tint apolloPatients= apollo.getNumberOfPatients();\r\n\r\n\t\tSystem.out.println(\"Health India hospital patients: \"+ healthIndiaPatients);\r\n\r\n\t\tSystem.out.println(\"IVY hospital Patients: \"+ iVYPatients);\r\n\r\n\t\tSystem.out.println(\"Apollo hospital Patients: \" +apolloPatients);\r\n\r\n\t}\r\n\r\n}<\/pre>\n<h4><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h4>\n<pre>Health India hospital patients: 1657\r\nIVY hospital Patients: 2965\r\nApollo hospital Patients: 1631\r\n<\/pre>\n<p>Now we have seen different hospitals are calling same method, but at run time it has to be decided which class method is called depending on which class object is calling the <strong><span style=\"color: #000000;\">getNumberOfPatients()<\/span><\/strong> method.<\/p>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Advantages of using Method Overriding:<\/strong><\/span><\/h4>\n<ul>\n<li>Method overriding is made to achieve Dynamic Binding or Runtime polymorphism.<\/li>\n<li>Method overriding is used to give more specific definition to the method which is already defined in the Base Class.<\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Rules of Method Overriding :<\/strong><\/span><\/h4>\n<ul>\n<li>Extended class must have same name of method as in base class.<\/li>\n<li>Method which is overridden must have same parameters.<\/li>\n<li>Method which is to be overridden should not be declared Final and static.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong><span style=\"color: #008000;\">Difference between Method Overloading and Method Overriding:<\/span><\/strong><\/h4>\n<p><strong><span style=\"text-decoration: underline; color: #000000;\">Method overloading:<\/span><\/strong><\/p>\n<ul>\n<li>Method Overloading happens at compile time.<\/li>\n<li>Static methods can be overloaded i.e a class can have more than one static method of same name.<\/li>\n<li>Method Overloading is done in the same class.<\/li>\n<li>Method Overloading gives better performance.<\/li>\n<li>In case of Method Overloading, return type of a method does not matter (means it will be same or different).<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline; color: #000000;\"><strong>Method Overriding:<\/strong><\/span><\/p>\n<ul>\n<li>Method Overriding happens at run time.<\/li>\n<li>Static methods cannot be overridden.<\/li>\n<li>For method overriding base and child class is required. Overriding is all about giving a specific implementation to the inherited method of a super class.<\/li>\n<li>Method Overriding gives less performance as compared to method overloading. The reason is binding of overridden method is being done at run time.<\/li>\n<li>In case of Method Overriding, Overriding methods \u00a0have more specific return type.<\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Important Points: <\/strong><\/span><\/h4>\n<ul>\n<li>Final method can\u2019t be overridden, as final is a keyword which when applied to class prohibits it from extending in other class, when applied to method it prohibits from overriding and when applied to keyword it prohibits from getting changed.<\/li>\n<li>Static method can\u2019t be overridden as static method belongs to the class and not to object of that class.<\/li>\n<li>We cannot override main method, as it is static method.<\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Quick Revision points:<\/strong><\/span><\/h4>\n<ul>\n<li>Method overriding is also called as Run time Polymorphism or Dynamic Binding.<\/li>\n<li>It increases the readability of code.<\/li>\n<li>It helps classes to be more specific in nature.<\/li>\n<li>Final and Static methods cannot be overridden.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overriding means to\u00a0extend\u00a0or to\u00a0pass\u00a0over something, especially to\u00a0overlap the previous described functionality. So Method Overriding means to re-write the previous described method again of Parent Class in Sub class with different functionality. In Method Overriding, we overrides the method of Super Class in Sub Class. The method overriding is also called Run time Polymorphism or Dynamic &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/method-overriding\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Method Overriding Tutorial With Examples In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-533","page","type-page","status-publish","hentry"],"psp_head":"<title>Method Overriding Tutorial With Examples In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Understand the complete concept, syntax and importance of Method Overriding with examples in JAVA. Learn how methods of Parent class is overridden in Sub class getting new functionality.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/method-overriding\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/533","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=533"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/533\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}