While implementing singleton pattern we have two options.
1. Private constructor. - Can not be instatiated.
2. Protected constructor.- can be instatiated only by subclass.
Give me practical example for the second case i.e what is the use of Protected constructor in sigleton pattern.
Avishkar Nikale
Ranch Hand
Joined: Aug 06, 2010
Posts: 173
posted
0
Constructors are not inherited so its just the visibility which matters.
It means your sublcass & other classes in your package can create objects of your Singleton class.
Avishkar Nikale wrote:Constructors are not inherited so its just the visibility which matters.
It means your sublcass & other classes in your package can create objects of your Singleton class.
Ranchers,
Please correct me if I am wrong.
Sub classes cannot create instances*. It cannot use protected constructors to create instances of the super class. Nor can it call protected methods on instances of the super class. The protected means that it can access the constructor in its own constructor (super(...)) and can call the protected methods on its own instances.
Suraj Behera wrote:Give me practical example for the second case i.e what is the use of Protected constructor in sigleton pattern.
I dont see any use of this, unless you allow someone to subclass; typically singleton has private constructor.
Avishkar Nikale
Ranch Hand
Joined: Aug 06, 2010
Posts: 173
posted
0
Rob Prime wrote:
Sub classes cannot create instances*. It cannot use protected constructors to create instances of the super class. Nor can it call protected methods on instances of the super class. The protected means that it can access the constructor in its own constructor (super(...)) and can call the protected methods on its own instances.