No, Static methods can’t be overridden because they are associated with class not with the object.
class MultiplicationTest { public static void multiplication(int num1, int num2) { System.out.println(num1 * num2); } } public class Main extends MultiplicationTest { public void multiplication(int num1, int num2) { System.out.println(num1 * num2); } public static void main(String[] args) { Main main = new Main(); main.multiplication(15, 5); } } |
Output
Main.java:11: error: multiplication(int,int) in Main cannot override multiplication(int,int) in MultiplicationTest public void multiplication(int num1, int num2) { ^ overridden method is static 1 error |
Note: Static method will be inherited, so if we want to use them we can use as static methods present in sub class. But we can not override them.
Example
class MultiplicationTest { public static void multiplication(int num1, int num2) { System.out.println(num1 * num2); } } public class Main extends MultiplicationTest { public static void main(String[] args) { Main.multiplication(15, 5); } } |
Output
75 |
Java interview questions on method overloading and overriding
- What is method overloading in java?
- Can we declare an overloaded method as static and another one as non-static?
- Can overloaded methods be synchronized?
- Synchronized override method
- Can we declare overloaded methods as final?
- Can overloaded method be overridden?
- What is method overriding in java?
- Can static method be overridden?
- Difference between method overloading and overriding in java?
- Can we override private methods in java?
- Is it possible to override non static method as static method?