by BehindJava

Can You Override Static Method in Java

Home » java » Can You Override Static Method in Java

The answer to this question is No, you cannot override the static method in Java because the method overriding is based upon dynamic binding at runtime and static methods are bonded using static binding at compile time. This means static methods are resolved even before objects are created, that’s why it’s not possible to override static methods in Java. Though you can declare a method with the same name and method signature in the subclass which does look like you can override static methods in Java but in reality that is method hiding.

Method overriding is an object-oriented concept that is based upon method resolution at runtime depending upon which object is calling the method rather than which class variable is holding the reference.

Java won’t resolve the static method call at runtime and depending upon the type of Object which is used to call static methods, the corresponding method will be called. It means if you use Parent class’s type to call a static method, original static will be called from a patent class, on the other hand, if you use Child class’s type to call static methods, the method from child class will be called.

In short, you can not override static method in Java. If you use Java IDE like Eclipse or Netbeans, they will show a warning that the static method should be called using class name and not by using object because a static method can not be overridden in Java.

Overriding Static method in Java - Example

In the last section, you learn the theory that we can not override static methods in Java, the static method can only be hidden in sub class. Also, don’t get confused between overloading and overriding as you can overload a static method in Java but you cannot override it.

Let’s see an example to test that theory which says you can not override static method in Java.

erer

/**
 *
 * Java program which demonstrates that
 * you can not override static methods in Java.
 * Had Static method can be overridden,
 * with Superclass type and sub class object
 * static method from subclass would be called in our example,
 * which is not the case.
 */
public class CanWeOverrideStaticMethod {
 
    public static void main(String args[]) {
     
        Screen scrn = new ColorScreen();
     
        //if we can  override static method in Java then
        // this should call the method from Child class

        //IDE like Eclipse or IDEA will show a warning,
        //the static method should be called from the class name

        scrn.show();
     
    } 
 
}

class Screen{
 
    /*
     * public static method which can not be overridden in Java
     */
    public static void show(){
        System.out.printf("The static method from parent class");
    }
}

class ColorScreen extends Screen{
    /*
     * static method of the same name and method signature
     * as existed in super
     * class, this is not method overriding instead this is called
     * method hiding in Java
     */
    public static void show(){
        System.err.println("Overridden static method
                 in Child Class in Java");
    }
}

Output:
The static method from the parent class

This output confirms that you can not override the static method in Java and the static method is bonded on compile-time, based upon type or class information and not based upon Object. That’s why static methods are also called “class methods” because they belong to the class.

Had the Static method be overridden, the method from the Child class of ColorScreen would have been called, which wasn’t the case here.

That’s all on discussion Can we override the static method in Java or not. We have confirmed that no, you can not override a static method, we can only hide the static method in Java. Creating a static method with the same name and method signature is called Method hiding in Java.