Why static method cannot be overridden in Java

Can a static method be overridden in Java, or can you override and overload static method in Java, is a common Java interview questions mostly asked to 2 years experienced Java programmers. The answer is, No, you can not override static method in Java, though you can declare a method with the same signature in a subclass. It won't be overridden in the exact sense, instead, that is called method hiding. But at the same time, you can overload static methods in Java, there is nothing wrong with declaring static methods with the same name, but different arguments. Some time interviewer also ask, Why you can not override static methods in Java? The answer to this question lies in the time of resolution.

As I said in the difference between static and dynamic binding, static methods are bonded during compile time using Type of reference variable, and not Object. If you have using IDE like Netbeans and Eclipse, and If you try to access static methods using an object, you will see warnings. As per Java coding convention, static methods should be accessed by class name rather than an object. 

In short, a static method can be overloaded, but can not be overridden in Java. If you declare,  another static method with same signature in derived class than the static method of superclass will be hidden, and any call to that static method in subclass will go to static method declared in that class itself. This is known as method hiding in Java.

Why static method cannot be overridden in Java
Let's see an example of trying to override a static method. In this Java The program, we have two classes Parent and Child, both have name() method which is static

Now, As per rules of method overriding, if a method is overridden than a call is resolved by the type of object during runtime. This means, in our test class StaticOverrideTest, p.name() in the second the example should call Child class' name() method because the reference variable of type Parent is referring an object of Child, but instead, it call name() method of Parent class itself. 

This happens, because static methods are resolved or bonded during compile time, and only information that is available, and used by the compiler is a type of reference variable. Since p was a reference variable of the Parent type, the name() method from the Parent class was called. Now, In order to prove that static method can be hidden, if we call Child.name() or c.name(), it will call name() method from Child class. 

This means static methods can not be overridden in Java, they can only be hidden. This also answers, Why the static method can not be overridden in Java, because they are resolved during compile time. By the way, this example doesn't show, whether you can overload static method or not, but you can. See this tutorial, for an example of an overloading static method in Java. 

/** * Java Program to show that, you can not override static method in Java. * If you declare same method in subclass then, It's known as method hiding. * * @author Javin Paul */ public class StaticOverrideTest { public static void main(String args[]) { Parent p = new Parent(); p.name(); // should call static method from super class (Parent) // because type of reference variable // p is Parent p = new Child(); p.name(); // as per overriding rules this should call to child's static // overridden method. Since static method can not be overridden // , it will call parent static method   // because Type of p is Parent. Child c = new Child(); c.name(); // will call child static method because static method // get called by type of Class } } class Parent{ /* * original static method in super class which will be hidden * in subclass. */ public static void name(){ System.out.println("static method from Parent"); } } class Child extends Parent{ /* * Static method with same signature as in super class, * Since static method can not be overridden, this is called * method hiding. Now, if you call Child.name(), this method * will be called, also any call to name() in this particular * class will go to this method, because super class method is hidden. */ public static void name(){ System.out.println("static method from Child"); } } Output static method from Parent static method from Parent static method from Child

That's all on this Java interview question guys. Remember, Static methods can not be overridden in Java, but they can be overloaded and hidden in Java. We have also touched based on What is method hiding in Java, and learned Why Static method can not be overridden in Java, since they are bonded during compile time by using a type of Class, and not at runtime using Objects.


Related Java Interview Questions from Javarevisited Blog

Why wait and notify methods are declared in Object class?

Why multiple Inheritance is not supported in Java?

Difference between Synchronized and ReentrantLock in Java

Why main method is public, static and void in Java?

Why Character array is preferred over String for storing password in Java

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.

Why static method cannot be overridden 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 ⇒