A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Inheritance is possible yes. Whether your constructor is private or not. You only inherit your super classes public and protected methods, not your super classes construstors. And if your super class is not a singleton, then anyone can instantiate your super class only, not your singleton. Your singleton will have it's own instance of its super class.
Perhaps I'm missing something, but if a class only has private constructors, how are you going to extend it? Can you provide a compileable example of a class that extends the following?Inheritance is possible yes. Whether your constructor is private or not.
Inheritance should not be allowed of a Singleton. It wouldn't be a Singleton if clients could instantiate a bunch of new subtypes.Correct me if I am wrong, inheritance is possible in singleton class, only if the singleton class should not be a super class.
The Singleton pattern has several advantages over the "static class" pattern. First, a singleton can extend classes and implement interfaces, while a static class cannot (well, it can extend classes, but it does not inherit their instance members). A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded. A sinlgeton class can be extended and it's methods overidden.
Perhaps the most important advantage, though, is that singletons can be handled polymorphically without forcing their users to assume that there is only one instance. For instance, assume you have a Configuration class that holds some global configs. Methods that use this configuration information may be defined as:
public void doSomething(Configuration config) {...}
When you start writing your system you may have only one global instance, so you make Configuration a singleton. But at some later point you may want to support more than one configuration set. Maybe you'd want to allow the user to load a Configuration object from and external file or or programmaticaly create his own (this happened in my code several times). Provided that the classes or methods that use a configuration object allow the user to pass his own instance, most of your code need not be aware of the "global" nature of the class and is therefore more flexible.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi