Amutha
Amutha

Reputation: 103

Private constructor and public parameter constructor

I heard that a private constructor prevents object creation from the outside world.

When I have a code

public class Product
{
   public string Name { get;set;}
   public double Price {get;set;}
   Product()
   {
   }

   public Product(string _name,double _price)
   {
   }
}

Here I still can declare a public constructor (parameter), won't it spoil the purpose of the private constructor? When do we need both private and public constructor (parameter) in code?

I need a detailed explanation please.

Upvotes: 9

Views: 7693

Answers (5)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391346

Constructors can be chained together to avoid having to duplicate code. It is quite common to have private constructors, that nobody is supposed to call outside of the class, that are chained from a public constructor.

Example:

public class Test
{
    private Test(int? a,string b) { }
    public Test(int a) : this(a, null) { }
    public Test(string b) : this(null, b) { }
}

Here there are two public constructors, one taking a string and one taking an int. They both chain to the common private constructor that takes both arguments.

Also, you can construct new objects from within the same class by using the private constructor. For instance, when you want specialized constructors only available through static factory methods:

public static Test Create()
{
    int? a = ReadConfigurationForA();
    string b = ReadConfigurationForB();
    return new Test(a, b);
}

When it is not be a good idea to expose a private constructor to the outside world, add a static factory method that fetches the correct arguments to pass on the constructor.

Upvotes: 5

Joseph
Joseph

Reputation: 25523

The reason you would use the pattern you're describing is when you want to control how the object is instantiated.

In your example, for instance, you're saying the only way to create a Product is by specifying its name and price. This is with respect to the outside world, of course. You could also do something similar using other access modifiers, and it would have different implications, but it all boils down to controlling how you want the objects instantiated with respect to who will be doing it.

If you wanted to prevent object creation altogether you would have to make all your constructors private (or protected). That would force the object to be created from within itself (or an inherited class).

Also, as Matti pointed out in the comment below, when you define a constructor that is parameterized you don't need to specify a private default constructor. At that point it is implied.

Upvotes: 5

ajdams
ajdams

Reputation: 2314

A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.

For more details refer to this: http://msdn.microsoft.com/en-us/library/kcfb85a6(VS.80).aspx

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115488

You need a private constructor when you only want that constructor to be called from within the class itself. In your example you are forcing the calling object to provide 2 parameters when creating the object.

With a private constructor you could do something like:

public static GetInstance ()
{
   return new YourObject();
}

but nothing else except the object could call the parameterless constructor.

It's commonly used to create a singleton pattern:

http://www.dofactory.com/Patterns/PatternSingleton.aspx

Upvotes: 2

Thomas
Thomas

Reputation: 64645

You would use a constructor with parameters when you wanted to force calling code to pass a value to the constructor in order to create an instance of your class. In your example, calling code must use the parameter version of the constructor in order to create a Product.

Upvotes: 0

Related Questions