views:

30

answers:

3

HI,

In singleton class we are declaring constructors as private. Is it possible to give as protected.

If giving as protected is it beneficial.

Whats the advantage or disadvantage of private over protected

A: 

If you make constructor private you can't make an object of that class from outside the class.If you make it as protected you can only use that constructor in subclasses of that class.

Srinivas Reddy Thatiparthy
+1  A: 

If you make the constructor protected, then any class inheriting from it could instantiate it multiple times. That would no longer make it a singleton.

Before you make a class into a singleton, thing hard if you really need to (do you really have to have a single instance of it in the whole application? Really?).

Oded
+1  A: 

If you really want to have a singleton you have to avoid that "someone else" can create an instance of the class. That's why it should be private, and the class should be sealed, which makes protected just the same as private since there is no inheritance possible.

Lucero