0

This is a bit weird and might ring of iffy syntax but hold with me. I've been trying for three months and I'm convinced that I need a way to do this:

public abstract class Sup{
    ...
    //This is implemented here because I cannot create an abstract static
    //only implemented by the children but called statically by methods in
    //the parent (more info later on in the post):
    protected static Class<? extends Sup> getTypeClass(){ return Sup.class };
    public static void init(){
        ...
        alreadyDeclaredHashMap.put(getTypeClass(), hashMapOfOtherStuff);
    }
}

public class A extends Sup{
    static{
        init();
    }
    protected static void getTypeClass(){ return A.class };
}
public class B extends Sup{
    static{
        init();
    }
    protected static void getTypeClass(){ return B.class };
}
... and so on.

So that if I were to print out alreadyDeclaredHashMap, it would look like:

    class A -> hashMapOfOtherStuff
    class B -> hashMapOfOtherStuff
    class C -> hashMapOfOtherStuff
    ...

But instead it prints:

    class Sup -> hashMapOfOtherStuff
    class Sup -> hashMapOfOtherStuff
    class Sup -> hashMapOfOtherStuff
    ...

Because the extending classes hide getTypeClass() but can't override it. This is just an example. In reality, I am making a Units system and I have a lot of methods depending on getTypeClass() and would really love to not have to rewrite them in every extending class (of which there are an indefinite number) with the only difference in implementation being the class name.

Many thanks!

P.S. These methods do have to be static because they are being called statically (and I would rather not have to create a dummy instance or reflection just to call them).

  • No, this is terrible design. Please provide the "Units" use case, so we can help you with that. This is an XY-problem. – Sotirios Delimanolis Aug 20 '15 at 01:52
  • *I've been trying for three months and I'm convinced that I need a way to do this*; **after** trying for three months, why are you so adamantly convinced? – Elliott Frisch Aug 20 '15 at 01:59
  • How can methods with return type void return anything? When overriding add @Override annotation to make sure that you are overriding something from the superclass. Also static methods cannot be overridden in Java http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods – Aseem Bansal Aug 20 '15 at 02:33
  • What do you mean depending on getTypeClass()? They have switch case? If yes then it would not matter having to rewrite them. If no then share your use case. It may be better done via strategy pattern or generics. Not sure what can be the exact one unless you provide the problem instead of your solution. – Aseem Bansal Aug 20 '15 at 02:35

1 Answers1

0

There is no way to get that to work. The static code in class sup has no knowledge of class A and class B, even when the init method is invoked from one of them.

Static methods are not "virtual", so calling getTypeClass() from the static code in Sup will call that implementation, not any of the subclass implementation.

Now, if you want to reuse the init method from A and B, you'll have to pass as parameters.

public abstract class Sup{
    ...
    public static void init(Class<? extends Sup> typeClass) {
        ...
        alreadyDeclaredHashMap.put(typeClass, hashMapOfOtherStuff);
    }
}

public class A extends Sup {
    static {
        init(A.class);
    }
}
public class B extends Sup {
    static {
        init(B.class);
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247