In Java, a static method cannot be abstract. Doing so will cause compilation errors.
The above code is incorrect as static methods cannot be static. When run, the Compilation Error that occurs is:
prog.java:12: error: illegal combination of modifiers: abstract and static abstract static void func(); ^ 1 error
What will happen if a static method is made abstract?
Assuming we make a static method abstract. Then that method will be as:
public abstract static void func();
- Scenario 1: When a method is described as abstract, using the abstract type modifier, it becomes as the subclass responsibility to give its implementation because they have no implementation specified in the super-class. Thus, a subclass must override them to provide method definition.
- Scenario 2: Now when a method is described as static, it makes it clear that this static method cannot be overridden by any subclass (It makes the static method hidden). So a static method cannot be overridden by the subclass, as static members are compile-time elements and overriding them will make it runtime elements (Runtime Polymorphism).
Now considering Scenario 1, if the func method is described as abstract, it must have a definition in the subclass. But according to Scenario 2, the static func method cannot be overridden in any subclass and hence it cannot have a definition then. So both the scenarios seem to contradict each other. Hence our assumption for static func method to be abstract fails.
Hence it is clear that the static method cannot be abstract.
Then that method will be as:
public static void func();
Static method implemented. Abstract method implemented.
- Static methods vs Instance methods in Java
- Abstract Methods in Java with Examples
- Can we Overload or Override static methods in java ?
- Understanding "static" in "public static void main" in Java
- Static and non static blank final variables in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- abstract keyword in java
- Abstract Classes in Java
- Difference between Abstract Class and Interface in Java
- Static blocks in Java
- Initialize a static Map using Java 9 Map.of()
- Static import in Java
- static keyword in java
- Static class in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.