0

I want each class that implements an interface to have a static factory method, as defined by the interface. I.e:

public interface Handle {
    public static Handle GetHandle() {
       return null;
    }
    public void DoThings();
}

public class HandleA implements Handle {
    private HandleA();
    public static HandleA GetHandle() {
       return new HandleA();
    }
    public void DoThings() {
       return;
    }
}

// This is allowed even though HandleB doesn't provide a GetHandle()
public class HandleB implements Handle {
    private HandleB();
    public void DoThings() {
       return;
    }
}

Reading past questions, it seems like the static method GetHandle() isn't enforced in concrete classes of Handle because that's not what the intended design behavior of static interface method is. Is there another way to do what I want? (i.e enforce all implementations of Handle to provide a "factory" GetHandle() method).

J.Doe
  • 293
  • 1
  • 5
  • 15

1 Answers1

0

You cant enforce a class to have a static method in Java.

Fermat's Little Student
  • 4,821
  • 4
  • 43
  • 66
  • and you can't abstract them either: https://stackoverflow.com/questions/370962/why-cant-static-methods-be-abstract-in-java – Irwan Hendra Aug 04 '17 at 01:50