{"id":1756,"date":"2016-09-23T18:20:38","date_gmt":"2016-09-23T18:20:38","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1756"},"modified":"2016-09-23T18:20:38","modified_gmt":"2016-09-23T18:20:38","slug":"why-java-allows-overriding-of-static-methods","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/why-java-allows-overriding-of-static-methods\/","title":{"rendered":"Why  Java allows overriding of static methods?"},"content":{"rendered":"<p><strong>Static methods cannot be overridden but can be redefined in child Class<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Animal \r\n{\r\n  static void doStuff() \r\n  {\r\n    System.out.print(&quot;animal&quot;);\r\n  }\r\n}\r\n\r\nclass Dog extends Animal \r\n{\r\n   \/\/ it's a redefinition\r\n   \/\/ not an override\r\n   static void doStuff() \r\n   { \t\t\t\t\t\r\n     System.out.print(&quot;dog&quot;);\r\n   }\r\n\r\n   public static void main(String &#x5B;] args) \r\n   {\r\n      Animal &#x5B;] a = {new Animal(), new Dog(), new Animal()};\r\n     \r\n      for(int x = 0; x &lt; a.length; x++)\r\n     \ta&#x5B;x].doStuff();               \/\/ invoke the static method \r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nanimal animal animal\r\n<\/pre>\n<p>Method overriding is made possible by <strong>dynamic dispatching<\/strong>, meaning that the declared type of an object doesn&#8217;t determine its behavior, but rather its runtime type<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nAnimal lassie = new Dog();\r\nlassie.speak(); \/\/ outputs &quot;woof!&quot;\r\nAnimal kermit = new Frog();\r\nkermit.speak(); \/\/ outputs &quot;ribbit!&quot;\r\n<\/pre>\n<p>Even though both lassie and kermit are declared as objects of type Animal, their behavior (method .speak()) varies because dynamic dispatching will only bind the method call .speak() to an implementation at run time &#8211; not at compile time.<\/p>\n<p>Now, here&#8217;s where the static keyword starts to make sense: the word &#8220;static&#8221; is an antonym for &#8220;dynamic&#8221;. So the reason why you can&#8217;t override static methods is because there is no dynamic dispatching on static members &#8211; because static literally means &#8220;not dynamic&#8221;. If they dispatched dynamically (and thus could be overriden) the static keyword just wouldn&#8217;t make sense anymore.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Static methods cannot be overridden but can be redefined in child Class class Animal { static void doStuff() { System.out.print(&quot;animal&quot;); } } class Dog extends Animal { \/\/ it&#8217;s a redefinition \/\/ not an override static void doStuff() { System.out.print(&quot;dog&quot;); } public static void main(String &#x5B;] args) { Animal &#x5B;] a = {new Animal(), new&hellip; <a href=\"https:\/\/codethataint.com\/blog\/why-java-allows-overriding-of-static-methods\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193,104],"tags":[],"class_list":["post-1756","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java","category-static-and-final"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1756"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1756\/revisions"}],"predecessor-version":[{"id":1757,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1756\/revisions\/1757"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}