mardi 15 mars 2016

Alternative of static abstract methods in Java

I'm in a situation where I need all my child classes to implement a common abstract method of a parent class...statically. But apparently Java does not allow abstract static methods...

So, what is an alternative design of such implementation in Java? Below is the code that describes the situation I am in:

Parent class

public abstract class ParentFactory {
    /**
    * This is a factory class that provides static init method
    **/
    public abstract static void init();
}

Child class

public class ChildFactory extends ParentFactory {
    @Override
    public static void init() {
        // each child factory has own implementation of init
    }
}


EDIT

FYI, I am not asking why this is not valid in Java, I realise the concept of a static method, I know why it is not allowed in Java.

Instead I am asking for help with an alternative design pattern regarding my approach above. Thanks!

Aucun commentaire:

Enregistrer un commentaire