1

I want to have the family of classes which all have the same method. As this method is quite simple I want to make it final static, something like

class A {
   public static final String createSth () { }
}

(in fact this method returns only String).

Sometimes it would be useful to upper-cast all those A, B, C classes with the same method. Thus I would like to create mother class.

class abstract Mother {
       public static abstract String createSth () { }
}

and then to add the appropriate extends for all my classes A, B, C ex. class A extends Mother.

Unfortunately it's not allowed by Java. I'm wondering why?

Sure I can remove static final modifier and then everything is ok. But on another hand if each subclass returns the constant String which is not modifiable in any way, why not?

You can ask what is the purpose of such a construction?

Simply I want to create database sql strings. For each table I created a separate class, and as a result of createSth method I want to return sql creating a table for this class.

Interfaces does help neither.

The only solutions seems to be to remove abstract modifier from createSth method in Mother class. But then I'm not allowed to uppercast to Mother class and to call createSth method for children. So I have to remove static modifier for children classes.

Concluding why abstract method is not allowed to be static? Ok here I can even understand that, but either why non static abstract method is not allowed to be replaced by static method in child class?

user2707175
  • 1,133
  • 2
  • 17
  • 44

3 Answers3

5

static methods (just like final and private) can't be overriden so there is no way to implement them in derived class so making them abstract would not make sense.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

Static methods don't get inherited, so making them abstract would be meaningless.

Eric Stein
  • 13,209
  • 3
  • 37
  • 52
0

final methods do not get overridden

static methods do not get overridden, but they can be hidden in the subClass.

JNL
  • 4,683
  • 18
  • 29