No, we cannot override non static method as static method in java.
class ShowTest { void show() { System.out.println("Inside super class method"); } } public class Main extends ShowTest { static void show() { System.out.println("Inside sub class method"); } public static void main(String[] args) { Main.show(); } } |
Output
Main.java:11: error: show() in Main cannot override show() in ShowTest static void show() { ^ overriding method is static 1 error |
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?