abstract class AbstractCase2 {
abstract static void simpleMethod1();//giving error
}
class Case2 extends AbstractCase2 {
static void simpleMethod1() {
System.out.println("Within simpleMethod1");
}
public static void main(String args[]) {
simpleMethod1();
System.out.println("with AwM");
}
}
Getting error:
C:\>javac Case2.java
Case2.java:8: error: illegal combination of modifiers: abstract and static
abstract static void simpleMethod1();
^
1 error
A static method belongs to class not to object instance thus it cannot be overridden or implemented in a child class. So there is no use of making a static method as abstract.
Declaring abstract method static If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.
An Abstract class can have access modifiers like private, protected, and internal with class members. But abstract members cannot have a private access modifier. An Abstract class can have instance variables (like constants and fields). An abstract class can have constructors and destructors.
1. Final abstract. Reason: Abstract methods must be overridden in a child class to provide implementation whereas final methods cannot be overridden. So, it is an illegal combination.
How can static method be abstract? static methods are not overridden!!
If you want to make your method abstract, make it non static
Abstract methods are designing constructs. You create abstract methods because you want your child classes to override them but static methods are associated with classes not their instance so they can't be overridden.
static abstract
makes your compiler bang its head.
You're pulling it from different directions.
static: Hey compiler, here's the code of my method, ready to be used whenever you need it, even if you don't create an instance.
abstract: Hey compiler, I'll put code in my method in the future, on one of the sub-classes.
Compiler: So do you have code or what? Oh no! Let me throw an error...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With