2

I have the abstract class Animal, and then I have classes like Dog and Cat, wich extend the class Animal.

I would like to make sure that all child classes of the parent class Animal have a static method: getProperties. Even when someone else, who doesn't have access to my code, implements a new Animal child class.

I want it to be static since all Animals of class Dog have the exact same properties (or you don't need a Dog to know how a Dog looks like genericly), and therefor it makes sense that it's a method called on the classtype, rather then a class instance.

Is this possible?

Don
  • 1,428
  • 3
  • 15
  • 31
  • 6
    `static` methods are not overridden , it belongs to a class .I believe you can't enforce. All *dogs* don't have same properties ! If it did , then you don;t need different instances of *Dog* !!! – AllTooSir Aug 01 '13 at 12:26
  • I think [this answer](http://stackoverflow.com/a/370967/1531054) is worth looking at. – S.D. Aug 01 '13 at 12:41

6 Answers6

3

A static in Java means that the method (for example) in not related to any of the instances of the class. It's common to all the instances and it's implementation is just nested within the class.

As I read you problem, I believe you need an abstract method in the Animal class.

public abstract class Animal {
    public abstract <some-return-type> getProperties();
}

In this case, every class which inherits Animal will have to provide an implementation of the getProperties method (which is not static in this case).

If you want it to be static, just make it so (within the Animal class), but then you will have a single and shared implementation.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

The Java Object model doesn't support class-side inheritance (unlike Smalltalk or Ruby). You have to explicitly implement a meta model of your domain, for instance, like this:

public abstract class AnimalDescriptor<T> { // or <T extends Animal>

    public abstract List<String> getProperties();

    public abstract T newAnimal();

}

public class DogDescriptor extends AnimalDescriptor<Dog> {

    public static final DogDescriptor INSTANCE = new DogDescriptor();

    public List<String> getProperties() { ... }

    public Dog newAnimal() { return new Dog(); }

}

void doStuff(AnimalDescriptor<?> desc) {
    desc.getProperties();
}

doStuff(DogDescriptor.INSTANCE);

This is just an example, you will have to adapt it to suit your needs. For instance, you might want to add a getDescriptor() method on the animal side.

Cephalopod
  • 14,632
  • 7
  • 51
  • 70
1

No, not possible. There's no inheritance in static contexts. And no, there's no way to enforce a class to implement a static method.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

That's not possible in Java, at much you could "hide" a static method, but not "override" it.

morgano
  • 17,210
  • 10
  • 45
  • 56
0

If you want to have each Animal class provide metadata about it, give it instance methods like getProperties, getNumberOfLegs, getLocomotionType, etc. You can implement these using static constants:

public abstract class Animal {
    public abstract int getNumberOfLegs();
}

public class Dog {
    private static final NUMBER_OF_LEGS = 4;
    public int getNumberOfLegs() {
        return NUMBER_OF_LEGS;
    }
    // ...
}

I suppose you wanted a Map of some sort for the getProperties method. The problem with that is that you must be very careful to make the map you return immutable. One mistake, and someone can change the static instance for everyone. making the method an instance method costs you almost nothing.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
0

Static methods need to be able to run without you explicitly creating a new instance of the Animal class. You will have to fully implement Animal.getProperties().

Lai
  • 472
  • 6
  • 23