Home > OS >  Correct ways to enforce implementation of inherited static methods in Java?
Correct ways to enforce implementation of inherited static methods in Java?

Time:04-05

My goal is to create an abstract class whose static methods have to be implemented, but I was having some issues due to static methods not being able to be made abstract.

This is the solution I came up with:

public abstract class combinedMethod {
    public static String computeMethodBody() throws
            CannotCompileException {
        throw new NotImplementedException();
    }
    public static ArrayList<CtMethod>  sortSelectedMethods() {
        throw new NotImplementedException();
    }
}

I'm making this post because I couldn't find any equivalent answer, which left me wondering if this is idiomatic in Java.

Edit to add use case:

I want to create several classes that must all implement both computeMethodBody and sortSelectedMethods. My solution adds structure and semantic meaning to the code, compared to, for example, creating documentation explaining how to create equivalent classes.

I am aware of why overriding static methods doesn't make sense in Java and that I'm just hiding them. As I said there's no other answer exemplifying this use case, but there's plenty discussing the concept.

CodePudding user response:

Static methods are invoked on a class, independent of an instance, so if they would be abstract, how would the run-time know on which sub-class to call them?

CombinedMethod cm1 = new SubclassA(...)
CombinedMethod cm2 = new SubclassB(...)

// static method computeMethodBody is called on neither cm1 or cm2, so what implementation to choose.
String result = CombinedMethod.computeMethodBody();

CodePudding user response:

enforce implementation of inherited static methods in Java?

Java Language Specification says:

A class does not inherit private or static methods from its superinterface types.

Static methods are also called class methods. They are bound to a class and don't require an instance of the class in order to be invoked.

static and abstract is an illegal combination of modifiers because static methods are completely self-contained and always have an implementation.

You can not inherit static methods and as subsequence the concept of overriding is not applicable to them. And since a subclass can not override a parent's static method, abstract static method doesn't make sense because it can not be implemented.

A class method can be hidden by a method from a subclass with the same signature.

A quote from JLS:

A class (static) method that is hidden can be invoked by using a reference whose type is the type of the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods.

I.e. hidden version can be invoked only on a child class or instance, conversely to the overridden method which can be invoked on the instance on of parent class or on the instance on of child class.

In other words, you can't obtain polymorphic behavior with static methods.

  • Related