Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If static methods can't be overridden, how its working here (For Java)?

My understanding was that static variables and static methods are of a class, not of the class objects. So an Override of a static method won't work in Java, as for overriding we need an instance of a class to be created. But I was trying something today that contradicted my knowledge of Java.

Please follow this code:

class Parent{    public static void doIt(){       System.out.println("In static method 'doit' of class Parent ");    } }  class Child extends Parent{        public static void doIt(){       System.out.println("In static method 'doit' of class Child ");    } }  public class StaticPractise{    public static void main(String[] args){       Parent.doIt();       Child.doIt();    }     } 

The output of the above implementation is:

D:\Rahul Shivsharan\MyPractise\JAVA>java StaticPractise In static method 'doit' of class Parent In static method 'doit' of class Child 

From this output I can understand that though the Child class extends the Parent class, the doit methods are individual to each class as they are static. So no overriding of them is allowed.

Now please follow the below code, where @Override is added to the child's doIt method:

class Parent{    public static void doIt(){       System.out.println("In static method 'doit' of class Parent ");    } }  class Child extends Parent{        @Override // Adding this annotation here    public static void doIt(){       System.out.println("In static method 'doit' of class Child ");    } }  public class StaticPractise{    public static void main(String[] args){       Parent.doIt();       Child.doIt();    }     } 

The output of the above code gives a compilation error as follows:

D:\Rahul Shivsharan\MyPractise\JAVA>javac StaticPractise.java StaticPractise.java:31: error: method does not override or implement a method from a supertype     @Override     ^ 1 error 

Here it clearly says that the annotation Override can't be applied in static methods as they are not getting overridden.

Now please follow the below code, where Child has no doIt method:

class Parent{    public static void doIt(){       System.out.println("In static method 'doit' of class Parent ");    } }  class Child extends Parent{ /* no doIt method */ }  public class StaticPractise{    public static void main(String[] args){       Parent.doIt();       Child.doIt();    }     } 

The output is:

D:\Rahul Shivsharan\MyPractise\JAVA>java StaticPractise In static method 'doit' of class Parent In static method 'doit' of class Parent 

Why does the above code compile and run? I was expecting a compilation error for method doit for class Child, and I was expecting "Method not found". I don't understand.

Also please follow the below code. Here, the doIt method in Parent is now final.

class Parent{    public static final void doIt(){ // now final       System.out.println("In static method 'doit' of class Parent ");    } }  class Child extends Parent{    public static void doIt(){       System.out.println("In static method 'doit' of class Parent ");    } }  public class StaticPractise{    public static void main(String[] args){       Parent.doIt();       Child.doIt();    }     } 

The output after running the above code is as follows:

D:\Rahul Shivsharan\MyPractise\JAVA>javac StaticPractise.java StaticPractise.java:30: error: doIt() in Child cannot override doIt() in Parent     public static void doIt(){                        ^   overridden method is static,final  1 error   D:\Rahul Shivsharan\MyPractise\JAVA> 

What I was expecting was that the above code should work fine as the doit methods are static in each class, so even a final keyword should not make any compilation error as the method is static.

Please explain to me how method overriding works in static classes in Java.

  1. Can static methods be overridden? If yes, then how come annotating @Override fails?
  2. If static methods can't be overridden, then how come my 3rd code block runs fine?
  3. If static methods can't be overridden, then how come the final keyword makes a difference?
like image 620
Rahul Shivsharan Avatar asked Aug 20 '15 08:08

Rahul Shivsharan


People also ask

Why static method can not be overridden in Java?

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call. Static methods are bonded at compile time using static binding. Therefore, we cannot override static methods in Java.

What happens if static method is overridden?

No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods. The calling of method depends upon the type of object that calls the static method.

What static method Cannot be overridden?

3) An instance method cannot override a static method, and a static method cannot hide an instance method.

Why private and static methods Cannot be overridden?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there.


2 Answers

First of all there are different mechanisms involved here: Overriding and Shadowing (also called hiding).

1) Static methods cannot be overriden as they are attached to the class they are defined in. However, you can shadow/hide a static method as you are doing with your Parent/Child class. This means, the method gets replaced in the Child class but is still available from the Parent class.

It gets more obvious that you are not overriding when you are calling the static methods from instances of those classes (and not using the Class.staticMethod() invocation).

Parent parent = new Parent(); Child child1 = new Child(); Parent child2 = new Child();  parent.StaticMethod(); child1.StaticMethod(); child2.StaticMethod(); 

the output is

Static method from Parent Static method from Child Static method from Parent 

The answer is the dispatch of the methods. You can grab the source code here

2) The dispatch finds the method on the Parent class. There is no dynamic dispatch as that the runtime type is used to find the method handle. It uses the compile time type. Remind: Calling static methods from instances is considered bad practice since things like above can happen and are easy to be overlooked.

3) With finalyou declare that the method cannot be overridden neither shadowed/hidden.

like image 126
lschuetze Avatar answered Sep 20 '22 23:09

lschuetze


You are confusing overriding with hiding

like image 35
Jordi Castilla Avatar answered Sep 21 '22 23:09

Jordi Castilla