Can We Override Static Method in Java?
In Java, overriding and overloading are the two most important features of object-oriented programming. The feature is used when the programmer wants to achieve polymorphism. The question, can we override static method in Java or can you overload static method in Java are the two most important question asked in Java interview. In this section, we will understand overloading and overriding in short. We have also explained the answer of why we cannot override the static method in Java?
The method that has a static keyword before the method name is known as a static method. It is also known as a class-level method. A copy of the static method is shared by all the objects of the class.
We can invoke static methods by using the class name. For example, Math.abs(a) method. The method returns the absolute value of the passed argument. The static method cannot access instance variables or methods.
It is a feature of object-oriented programming. It is used to achieve run-time polymorphism. The subclass provides a specific implementation of a method that is already provided by its parent class, known as method overriding. The signature of the method in parent and child class must be the same. In method overriding, which method is to be executed, decided at run-time. The decision is made according to the object that we called.
It is also a feature of object-oriented programming. It is used to achieve compile-time polymorphism. It allows us to use the same method name but different signatures. If a class has more than one method with the same name but a different method signature, it is known as method overloading.
We have learned what is overloading and overriding. Now we move to the point.
Can we overload a static method?
The answer is Yes. We can overload static methods. But remember that the method signature must be different. For example, consider the following Java program.
Static method called. An overloaded static method called.
Here a question arises that can we overload the methods if they are only different by static keyword.
The answer is No. We cannot override two methods if they differ only by static keyword. For example, consider the following Java program.
When we compile the above program, it shows the following error.
error: method sum(int,int) is already defined in class OverloadStaticMethodExample2
Can we override a static method?
No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods.
The calling of method depends upon the type of object that calls the static method. It means:
In the following example, the ParentClass has a static method named display() and the ChildClass also has the same method signature. The method in the derived class (ChildClass) hides the method in the base class. let's see an example.
display() method of the parent class.