Can We override Static method in Java in 3 Cases

No, we cannot override static method in Java because a static method is resolved at compile time by java compiler whereas,

method overriding is resolved at runtime by JVM because objects are only available at runtime.

We can declare static methods with the same signature in subclass, but they are not considered as overriding. They are considered a method hiding.

Look at 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 questions where the interviewer will try to confuse you. May be interviewer also does not know whether they can override or overload static methods 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 static methods 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(); 
  } 
}

Explanation:

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.

Hence, 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.

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(); 
  } 
}

Explanation:

In this example, m1() method in parent class is an instance method but a static method in child class.


In this case, java 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.

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

Explanation:

In this example program, both parent class and child class methods are static. We did 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 the output of the above program.

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 the 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 is “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 is “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 is “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.

We know that in overriding, a method resolution is always resolved by JVM based on runtime object because objects are only available at runtime.


Key Points: 

1. We cannot override a static method as non-static otherwise we will get a compile-time error.
2. We cannot override a non-static method as static.


Hope that this tutorial has covered three different cases related to the topic “can we override static method in Java” I hope that you will have understood all the important points.
Thanks for reading!!!
Next ⇒ Static block in Java⇐ PrevNext ⇒

Leave a Comment