The approach you were attempting is more commonly done in C++, where they have a few more tricks up their sleeves they can use to pull it off. In particular they can use templates to avoid the casting.
Yes.
You can of course just use all static methods and avoid the singleton object all together. That's also nice in some cases.
This is the way I do it now. Any singleton class has its own few lines of code for the handling of the static sInstance. No casting. However I'd still prefer an abstract singleton base class with no casting.
In my original inheritance example there's a mistake:
1 2 3
| abstract public class Singleton { private static Singleton sInstance; |
This won't work because any inherited class of the Singleton base class will use the same sInstance.
How would a real Singleton base class have to work in Java - with casting then?