I have some classes that are used as Singletons. They share some basic functionality and extend the same ancestor from a library which in turn isn't normally used as a Singleton.
If I put the common functionality in a base class that inherits from the common ancestor, I get a class which makes no sense to instantiate, so I made it abstract. Also, because the classes are all used as Singletons, they should all have an init() and a getInstance() method, which are both static. All the constructors are of course non-public.
Now, since static
is an illegal modifier for abstract methods, the following doesn't work, although this would be exactly what I want:
class Base extends LibraryClass {
protected Base() {
// ... constructor
}
// ... common methods
// ILLEGAL!
public static abstract void init();
public static abstract <T extends Base>T getInstance();
}
class A extends Base {
private static A _INSTANCE;
private A() {
super();
}
public static void init() {
_INSTANCE = new A();
}
public static A getInstance() {
return _INSTANCE;
}
}
I could just leave out the illegal lines in the base class and be done with it. But how do I express that every child of Base must have these methods?