-1

Ok, so I have a hierarchy of classes and subclasses. They all must have certain methods. Those methods are in an interface implemented by the superclass and everything below.

There is one method they should all have and define but that method is "in spirit", of the static kind, returning the same results for all instances of a given class.

Unfortunately, one cannot have a static method as part of an interface and also, static method do not support inheritance, they "hide" rather than "override".

So, what are the options for a static method that all classes in a hierarchy must have and that can be called in a generic way? At the moment, I made that method non static...

Also, ideally, that shouldn't involve having a second hierarchy of classes that must be kept in sync just for that one method...

Many Thanks

======

EDIT: People asked for an example so here you go:

OK, let's say i have a hierarchy of animal classes (abstract Animal class > abstract Mammal class > Cat class, Dog class, Dolphin class, etc). Some of the behaviour will be instance specific, they may have members like "weight", "name", "age", etc and have related getter/setter.

but then, there might be one method that would typically give the same result for all the instances of a given class, for example it could be one of those:

-int getNumberOfLegs();
-String getFavouriteFood();

All cats have 4 legs so that method could be static, it's not instance-dependant. it's more like meta information. I want to be able to do Cat.getNumberOfLegs(). I want to force the existence of that method on all my hierarchy in a way that can be overridden, etc.

MikaelW
  • 1,145
  • 4
  • 11
  • 29

2 Answers2

0

Though it shall be better if you can support your question with example. From what I understood is, you want subclasses to write their own behavior for certain method - thus you need overriding. Thus probably this method is not a candidate for being static. Probably what best you can do is, to have an abstract class. Provide a default implementation, if you really insist. Or Just make sure that every subclass defines it. For that make the method it self as abstract.

After EDIT: Quoting you:

I want to force the existence of that method on all my hierarchy in a way that can be overridden, etc.

Above is clearly indication that you need abstract. Not Static.

-int getNumberOfLegs();
-String getFavouriteFood();

Shall be different for dolphin and dog. So there is no way you can do it through static method. Static method can be for your mammel class where it can have something like

String getMyNature {
    // return Mammel nature
}

Static has to be class level. But since you want the same interface to access instance specific information, you shall have to use abstract (for forcing each implementation to have it).

Tyagi Akhilesh
  • 744
  • 6
  • 15
  • I added an example. Thank you. – MikaelW Dec 01 '15 at 14:24
  • but, within a given class, the method always give the same value independently of any instance. The Cat class has many cat instances but they all have 4 legs so this method could be called statically. Same for favourite food or any other behaviour when it's about the whole class and not a specific instance. I mean it might be just a limitation of the language. Inherited static/class methods in interface are supported in other languages. Was keen to see the good java workarounds. – MikaelW Dec 01 '15 at 15:16
  • then you can use static method inside every subclass. but static methods cannot be overriden. then your requirement for common interface cannot be satisfied – Tyagi Akhilesh Dec 01 '15 at 15:18
0

Apart of an abstract method in your Animal that would be overriden in each class to provide a constant value like

class Cat extends Animal {
    private static final int NUMBER_OF_LEGS_ON_A_CAT = 4;

    @Override
    public int getNumberOfLegs() {
        return NUMBER_OF_LEGS_ON_A_CAT;
    }
}

you can also create non-abstract method in your superclass and provide the constant through super constructor like this:

class Animal {
    private final int numberOfLegs;

    protected Animal(int numberOfLegs) {
        this.numberOfLegs = numberOfLegs;
    }        

    public final int getNumberOfLegs() {
        return numberOfLegs;
    }
}

class Cat extends Animal {
    private static final int NUMBER_OF_LEGS_ON_A_CAT = 4;

    public Cat() {
        super(NUMBER_OF_LEGS_ON_A_CAT);
    }
}

This approach may be less verbose, on the other hand can become quite unreadable if there's too many constants like this.


The bottom line is however that static methods and subtyping don't go well together, so the best you can do is probably make the method non-static and ensure that the data it returns is static - this is achieved by providing a constant through constructor into a final field, by returning a constant in the overriden method, etc.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43