6
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
tokland
  • 66,169
  • 13
  • 144
  • 170
Shivam Mishra
  • 73
  • 1
  • 5

3 Answers3

14

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.

Lokesh
  • 7,810
  • 6
  • 48
  • 78
9

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...

Jops
  • 22,535
  • 13
  • 46
  • 63
1

Abstract and static are an illegal combination because abstract methods don't have a body, and static methods can be called without an object of the abstract class. When we don't have a body for a function, how can we call the function? We cannot call a function with just a prototype without defining it.

Abstract functions can be non-static. This is because non-static functions need an object to be called and objects for a class which inherits from an abstract class can only be created if and only if it overrides all the abstract functions of its parents. So, we cannot create an object without giving a body to the parents abstract functions and thus resolving the ambiguity.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40