简体   繁体   中英

Java: static method in abstract class call abstract non-static method?

I have an abstract class A

I have about 10 classes that extend A

Class A has one or two static methods and it makes sense that these are static, because they belong to the 10 classes, NOT instances of them. One static method eg is called getAllFromX, which gets all all instances of the class from X, whatever that may be, it may be a server, well it actually is, but it doesn't matter. So you see it makes sense these methods are static and are not bound to an instance.

At the same time class A has a NON-static abstract method, each subclass overrides this method (just returns a string). I cannot make it static because static methods cannot be overridden (...).

To summarize: abstract class A has a static method and a abstract non-static method, that is overriden by the subclasses. I cannot make the second method static because it must be overriden. On the otherhand I could make the first method non-static, but it would be very ugly and bad programming style, so I'll leave it that way.

The catch? The static method in class A must get the value the non-static method returns (for the subclass the static method is inherited from, of course).

Is the "easiest" way to use reflection to get this done? I mean...really?

Like eg, I get the class the static method is in:

Class<?> cl=new Object(){}.getClass().getEnclosingClass(); (a hack I found here, thank god...)

I then use getConstructor to construct an object of this subclass.

And then I use this object to call the non-static method.

Really?? Can it not be done easier? I mean that is if I want to design my program conceptually correct...

Coming from C# I don't like that (and the type erasure thing). It is just ugly. Doable but ugly. And a big stumbling block, at least for beginners. EDIT: after reading it again, I'd add: /rant end. Sorry, but I actually care.

I think what you in fact need is the following:

public class A {
    public static Set<A> getAllFromX() {
        ...
    }
}

public class B extends A {
    public static Set<B> getAllFromX() {
        ...
    }
}


public class C extends A {
    public static Set<C> getAllFromX() {
        ...
    }
}

(Just as the valueOf() and values() methods in enums, which is redefined in every Enum subclass, because static methods can't be inherited)

In this case, each class has its own static method doing whatever it wants. But your question doesn't make much sense because it says:

The static method in class A must get the value the non-static method returns (for the subclass the static method is inherited from, of course).

Indeed, the static method is not inherited by the subclass. Static methods are never inherited. If you define a static method foo() in A, and call

B.foo();

the compiler doesn't refuse to compile it, but it translates it to

A.foo();

So, there's no way to do in foo() something that depends on the class on which foo() is called, since it's always A.

You can always use reflection to invoke a method using class name eg

    Object objectX = ClassX.class.newInstance();
    //get your method passing argument types as second param
    Method method = ClassX.class.getDeclaredMethod("methodX", null);
    //invoke your method passing arguments as second param
    method.invoke(objectX, null);

Since you mentioned your static method doesn't use any instance but you are using reflection to get the instance hence I am really not sure, how does it fit in your requirement though.

I think making it as an implemented method (non-static) in your abstract class is a better choice. That way you implement it once but its available in in all your 10 extending classes.

I think your problem is one of larger design. A different object should be responsible for retrieving instances of A or its subclasses. As you can see, relying on a static method to be replaced by subclasses does not work well. Without knowing more about the problem domain, it's hard to give a good answer, but I would consider something similar to the Abstract Factory pattern.

Broadly speaking: Define an abstract class, AFactory, with a method Collection getInstances(). Extend AFactory for each of the concrete subclasses of A you need to return and implement that logic in the overridden getInstances() method as appropriate. You may also provide a static method on the abstract AFactory, getFactory(Class), to get the appropriate factory subtype at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM