In Java, a static method cannot be abstract. Doing so will cause compilation errors.
The above code is incorrect as static methods cannot be abstract. 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 written as:
public abstract static void func();
- Scenario 1: When a method is described as abstract by using the abstract type modifier, it becomes responsibility of the subclass to implement it because they have no specified implementation 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) 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 the scenarios seem to contradict each other. Hence our assumption for static func method to be abstract fails. Therfore, a static method cannot be abstract.
Then that method will be coded as:
public static void func();
Static method implemented. Abstract method implemented.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Why non-static variable cannot be referenced from a static method in Java
- Understanding storage of static methods and static variables in Java
- Static methods vs Instance methods in Java
- Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
- Abstract Methods in Java with Examples
- Static and non static blank final variables in Java
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Internal static variable vs. External static variable with Examples in C
- Can we Overload or Override static methods in java ?
- Abstract Classes in Java
- Difference between Abstract Class and Interface in Java
- abstract keyword in java
- Difference between Abstract Class and Concrete Class in Java
- Difference between Final and Abstract in Java
- Messages, aggregation and abstract classes in OOPS
- Shadowing of static functions in Java
- Are static local variables allowed in Java?
- Assigning values to static final variables 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.
Improved By : pigenio