CAN WE OVERRIDE STATIC METHOD

No,we cannot override static method in Java because method overriding is based upon dynamic binding which will happen at runtime and static methods are bonded using static binding.

We can declare a method with same name and signature in a subclass,this is not method overriding instead it is method hiding..

Its means if Reference type is of Super class it will call super class method or if it is sub class type it will call subclass method..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class SuperClass{
    public static void methodtobeoverriden(){
        System.out.println("In SuperClass");
    }
}
public class StaticCannotBeOverriden extends SuperClass  {
    public static void methodtobeoverriden(){
        System.out.println("In SubClass");
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StaticCannotBeOverriden subclass=new StaticCannotBeOverriden();
        subclass.methodtobeoverriden();
         
        SuperClass superclass=new StaticCannotBeOverriden();
        superclass.methodtobeoverriden();//It will be give output of Super Class method.
    }
}
        

Output of the program is given below:

override static method