Subclassing Singleton class
When i googled with "Subclassing Singleton class" string, many of the blogs said, we cannot extend the Singleton class as the constructor is private. But when i tried it in eclipse, i am able to subclass a singleton class. Would like to know whether can we subclass a singleton class technically?? If yes, will it not create many instances of Singleton class?
My singleton class
class SingletonClass {
private static SingletonClass singletonObject;
/** A private Constructor prevents any other class from instantiating. */
private SingletonClass() {
// Optional Code
}
public static synchronized SingletonClass getSingletonObject() {
if (singletonObject == null) {
singletonObject = new SingletonClass();
}
return singletonObject;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
Re: Subclassing Singleton class
What happens when you try to compile and execute the testing code?
Re: Subclassing Singleton class
There was a small mistake in my singleton class. You r rite, it gives compiler error "Implicit super constructor SingletonClass() is not visible for default constructor. Must define an explicit constructor".
Thanks for ur help.