Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract static classes in java giving error illegal combination of modifiers

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
like image 247
Shivam Mishra Avatar asked Apr 14 '13 04:04

Shivam Mishra


People also ask

Why abstract and static combination is not allowed for method?

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.

Can we write a static modifier in an abstract class?

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.

Can abstract class have access modifiers?

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.

Which of the following modifier combination is invalid for a method in Java?

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.


2 Answers

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.

like image 121
Lokesh Avatar answered Sep 20 '22 13:09

Lokesh


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

like image 42
Jops Avatar answered Sep 19 '22 13:09

Jops