Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a private constructor?

Tags:

c#

.net

asp.net

If a class has a private constructor then it can't be instantiated. So, if I don't want my class to be instantiated and still use it, then I can make it static.

What is the use of a private constructor?

Also, it's used in the singleton class, but except for that, is there any other use?

(Note: The reason I am excluding the singleton case above is that I don't understand why we need a singleton at all when there is a static class available. You may not answer this for my confusion in the question. )

like image 811
Rasshme Chawla Avatar asked Apr 06 '10 14:04

Rasshme Chawla


People also ask

When would you use a private constructor in C++?

A private constructor in C++ can be used for restricting object creation of a constant structure. And you can define a similar constant in the same scope like enum: struct MathConst{ static const uint8 ANG_180 = 180; static const uint8 ANG_90 = 90; private: MathConst(); // Restricting object creation };

Should I make a constructor private?

You shouldn't make the constructor private. Period. Make it protected, so you can extend the class if you need to.

What is the use of private constructor in Singleton class?

In singleton class, we use private constructor so that any target class could not instantiate our class directly by calling constructor, however, the object of our singleton class is provided to the target class by calling a static method in which the logic to provide only one object of singleton class is written/ ...

Why do we need private constructor in Java?

Private constructors allow us to restrict the instantiation of a class. Simply put, they prevent the creation of class instances in any place other than the class itself. Public and private constructors, used together, allow control over how we wish to instantiate our classes – this is known as constructor delegation.


1 Answers

Factory

Private constructors can be useful when using a factory pattern (in other words, a static function that's used to obtain an instance of the class rather than explicit instantiation).

public class MyClass {      private static Dictionary<object, MyClass> cache =          new Dictionary<object, MyClass>();      private MyClass() { }      public static MyClass GetInstance(object data)     {         MyClass output;          if(!cache.TryGetValue(data, out output))              cache.Add(data, output = new MyClass());          return output;                } } 

Pseudo-Sealed with Nested Children

Any nested classes that inherit from the outer class can access the private constructor.

For instance, you can use this to create an abstract class that you can inherit from, but no one else (an internal constructor would also work here to restrict inheritance to a single assembly, but the private constructor forces all implementations to be nested classes.)

public abstract class BaseClass {     private BaseClass() { }      public class SubClass1 : BaseClass     {         public SubClass1() : base() { }     }      public class SubClass2 : BaseClass     {         public SubClass2() : base() { }     } } 

Base Constructor

They can also be used to create "base" constructors that are called from different, more accessible constructors.

public class MyClass {     private MyClass(object data1, string data2) { }      public MyClass(object data1) : this(data1, null) { }      public MyClass(string data2) : this(null, data2) { }      public MyClass() : this(null, null) { } } 
like image 180
Adam Robinson Avatar answered Sep 19 '22 07:09

Adam Robinson