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