Can we override Static method in Java?


No, we cannot override static method in Java because a static method is resolved at compile time by the compiler whereas method overriding is resolved at runtime by JVM because objects are only available at runtime.
You can declare static methods with the same signature in subclass, but it is not considered as overriding. It is considered a method hiding. See the difference between method overriding and method hiding in the below figure.
Can We override Static method in Java
This is one of the most tricky java's questions where the interviewer will try to confuse you. Maybe interviewer also does not know about whether they can override or overload a static method in Java. Therefore, you do not confuse and give an exact answer.

Let's understand from three different cases with example programs where we cannot override the static method in Java.
Case 1: 
Program source code 1:
    package overriding; public class AB { static void m1(){ System.out.println("m1-AB"); } } public class ABC extends AB{ protected void m1(){ System.out.println("m1-ABC"); } } public class TestABC { public static void main(String[] args) { ABC obj=new ABC(); obj.m1(); } }
In this example program, the m1() method is declared as static in the parent class but not in child class. We are trying to override a static method with a non-static method (instance method). So, Is it a valid case? No, it is not a valid case for overriding because static method means class-level method whereas instance method means object-level method. 

So, we cannot override a class-level method with an object-level method. But if you are trying to override class-level method with instance method, you will get a compile-time error: "m1() in ABC cannot override m1() in AB; overridden method is static". This is because the compiler detects that you are trying to override a static method with a non-static method that should be hidden and generates a compiler error.

Key point: 
We cannot override a static method as non-static otherwise we will get a compile-time error.

Case 2: 
Programs source code 2:
    package overriding; public class AB { public void m1(){ System.out.println("m1-AB"); } } public class ABC extends AB{ public static void m1(){ System.out.println("m1-ABC"); } } public class TestABC { public static void main(String[] args) { AB obj=new ABC(); obj.m1(); } }
In this example, m1() method in parent class is instance method but a static method in child class. In this case, the compiler thinks that you are trying to hide a method that should be overridden and generates a compile-time error: "m1() in ABC cannot override m1() in AB; overriding method is static". So, this case is also not valid for overriding. Therefore, we cannot override an instance method with a static method.
Key point: 
We cannot override a non-static method as static.



Case 3: 
Program source code 3:
    package overriding; public class AB { public static void m1(){ System.out.println("m1-AB"); } } public class ABC extends AB{ public static void m1(){ System.out.println("m1-ABC"); } } public class TestABC { public static void main(String[] args) { AB obj1=new AB(); obj1.m1(); ABC obj2=new ABC(); obj2.m1(); AB obj3=new ABC(); obj3.m1(); } }
    Output: m1-AB m1-ABC m1-AB
In this example program, both parent class and child class methods are static, we will not get any compile-time error. It seems overriding concept applicable for static methods but it is not overriding, it is method hiding. How?

Let's understand it by the output of the above example program.
Key points: 
In method hiding, a method resolution is always resolved by the compiler based on the reference type. Runtime object does not play any role in method hiding concept.

1. obj1.m1(); will call m1() method of class AB because the reference variable obj1 is the type of AB. Therefore, the output will be "m1-AB".

2. obj2.m1(); will call m1() method of class ABC because the reference variable obj2 is the type of ABC. Therefore, the output will be "m1-ABC".

3. obj3.m1(); will call m1() method of class AB because the reference variable obj3 is the type of AB. Therefore, the output will be "m1-AB". Suppose that it is method overriding then what will be the output?

In this case, the output will be "m1-ABC" because the reference variable obj3 is pointing to the object of class ABC and you know that in overriding, a method resolution is always resolved by JVM based on runtime object because objects are only available at runtime.
We will discuss the method hiding in more detail in the further tutorial.

Final words 
Hope that this tutorial has covered almost all the important points related to overriding static method in Java with 3 different cases. I hope that you have understood nicely this important topic and will have enjoyed.
 Thanks for reading!