Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Abstract static" method - how?

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?


EDIT: Here how I might use this. Assume (now this is ridiculous, but anyhow...) that the number of legs of each animal is unique, so I might have something like:

Animal getModelAnimal(int numberOfLegs) {
   if (numberOfLegs == Chicken.getNumberOfLegs()) return new Chicken();
   else if (numberOfLegs == Dog.getNumberOfLegs()) return new Dog();
}
like image 229
polyglot Avatar asked May 29 '10 23:05

polyglot


2 Answers

How do one usually go about implementing this situation?

The usual solution is to make the method in question an instance method.

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

That is emphatically not obvious! We don't program for a perfect world, and in the real world four-legged animals sometimes have one, two, or three (or five) legs.

If your program needs animal definitions rather than animal instances, go ahead and make a class for that.

class AnimalDefinition {
    public string getScientificName();
    public string getCommonName();
    public int    getNumberOfLegs();
    public bool   getIsAmphibious();
    // etc.
}

Then initialize a collection of those at the beginning of your program - ideally from a database or configuration file where you can add animal definitions without writing or compiling another line of code. (And you can get away with far fewer types.)

like image 174
Jeff Sternal Avatar answered Nov 13 '22 06:11

Jeff Sternal


Your pseudocode looked a lot like Java, so I'm going to assume it is Java you are using.

"An abstract method requires implementation per instance. Static methods pertain to an overall class. A static method in an abstract class belongs to the abstract class, not potential implementations. It therefore doesn't make any sense to allow abstract static methods. Furthermore, static methods cannot be overridden, so again, abstract static methods would be an anomaly."

From http://forums.sun.com/thread.jspa?threadID=597378

Please also look at Why can't I define a static method in a Java interface?

like image 42
Christian Neverdal Avatar answered Nov 13 '22 04:11

Christian Neverdal