Read chapter 19 to use interfaces only for determining types from Joshua Bloch Effective Java (in fact, please read the entire book)
Constants are not included in the interface !!! Constants should be tied to class implementations, not to interfaces.
Use fickle methods:
// the implementing classes can define these values // and internally use constants if they wish to public interface BaseInterface{ String id(); // or getId() String root(); // or getRoot() } public interface MyInterface1 extends BaseInterface{ void myMethodA(); } public interface MyInterface2 extends BaseInterface{ void myMethodB(); }
or use the listing to link things:
public enum Helper{ ITEM1(MyInterface1.class, "foo", "bar"), ITEM2(MyInterface2.class, "foo2", "baz"), ; public static String getId(final Class<? extends BaseInterface> clazz){ return fromInterfaceClass(clazz).getId(); } public static String getRoot(final Class<? extends BaseInterface> clazz){ return fromInterfaceClass(clazz).getRoot(); } private static Helper fromInterfaceClass(final Class<? extends BaseInterface> clazz){ Helper result = null; for(final Helper candidate : values()){ if(candidate.clazz.isAssignableFrom(clazz)){ result = candidate; } } return result; } private final Class<? extends BaseInterface> clazz; private final String root; private final String id; private Helper(final Class<? extends BaseInterface> clazz, final String root, final String id){ this.clazz = clazz; this.root = root; this.id = id; }; public String getId(){ return this.id; } public String getRoot(){ return this.root; } }
Sean Patrick Floyd
source share