"Abstract static" method - how?
- by polyglot
There are already several SO questions on why there is not abstract static method/field as such, but I'm wondering about how one would go about implementing the following psuedo-code:
class Animal {
abstract static int getNumberOfLegs(); // not possible
}
class Chicken inherits Animal {
static int getNumberOfLegs() { return 2; }
class Dog inherits Animal {
static int getNumberOfLegs() { return 4; }
Here is the problem: Assuming that I want make sure that every class that inherits Animal to contain getNumberOfLegs() method (i.e. almost like an interface, except I do want the abstract class to implement several methods that are common to all child classes, hence pure interface does not work here). getNumberOfLegs() obviously should be a static method (assuming that in a perfect world we dont' have crippled chicken and dogs so getNumberOfLegs is not instance-dependent).
Without an "abstract static" method/field, one can either leave the method out from Animal class, then there is the risk that some child class do not have that method. Or one can make getNumberOfLegs an instance method, but then one would have to instantiate a class to find out how many legs that animal has - even though it is not necessary.
How do one usually go about implementing this situation?