Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what are the real world example - use of private constructor and constructor overloading?
Posted
Updated 25-Jun-14 19:32pm
v2
Comments
Thanks7872 26-Jun-14 1:41am    
This is not a question. Tons of examples,explanation is already available out there.
Sergey Alexandrovich Kryukov 26-Jun-14 1:47am    
Just read a book on C++, understand the syntax and semantic and use some fantasy, and the examples should become obvious to you.
—SA

Pretty simple:
A private constructor allows you to create a class that can't be instantiated from outside the class: the outside world can't do this:
C#
MyClass mc = new MyClass();
The only way to create an instance is to call a static method of the class and have it return the instance.
This is mostly used for the Singleton pattern, where there is only ever a single instance of the class and it is "shared" by all code using it.

Constructor overloading allows you to create "versions" of your constructor which require specific parameters which must be supplied. For example you might create two overloaded constructors which let you pass a List of strings, and an array of strings:
C#
public MyClass(List<string> list)
   {
   ...
   }
public MyClass(string[] list)
   {
   ...
   }
So that your user can path either and you will still work fine.
(And yes, you could probably just create a single constructor that took an IEnumerable<string> - that isn't the point)
 
Share this answer
 
Comments
Abhinav S 26-Jun-14 2:27am    
5!
Aarti Meswania 26-Jun-14 2:35am    
5! :)
Most typical use of a private constructor is hiding of a default parameterless constructor. For example:
C#
public class Owner {/* ... */}

public class NotDirectlyInstantiatingObject {
    // let's say, we want to have only one method of instantiating of this class by the user,
    // to guarantee that each instance is added to some owner:
    public static NotDirectlyInstantiatingObject Create(Onwer owner) {/* ... */}
    // to prevent direct instantiation of this class, we need to disable the default constructor; here is how:
    private NotDirectlyInstantiatingObject() { } // it exists but cannot be called
}


There are very many designs of the code where the direct instantiation of the class is hidden from a user, different in the ways of public instantiations of such class, but the way of disabling of the parameterless default constructor is always the same.

For understanding the rules for default constructors, please read about it: http://msdn.microsoft.com/en-us/library/aa645608%28v=vs.71%29.aspx[^].

As to "constructor overloading", this is just the useless term. I would never use the confusing term "overloading" even for other methods, because nothing is really "loaded". Let's see. It actually simply means that the syntax allows to create different (formally unrelated) methods with the same name. A compiler recognizes them at the point of the call by actual parameters, and only if such determination is ambiguous, it generates the compilation error. Now, let's see: in C#, all constructors of the same class has the same name as the name of this class. It means that there are no "overloaded" or "non-overloaded" constructors; if you will, the are all "overloaded". So your question is reduced to the question: "what are the real world examples of using different constructors". Come on, such question would be ridiculous. Too many reasons; even for simple convenience of the class user…

I hope I answered both questions: provided some example of the use of the private constructor and explained why there is no a need to give example on those almost mythical "overloaded constructors". Also, please follow my advice you can find in my comment to the question.

—SA
 
Share this answer
 
Comments
Abhinav S 26-Jun-14 2:27am    
5!
 
Share this answer
 
Private constructor - a classic real world example is the Singleton pattern - Singleton pattern[^].
There are tons of examples of this implementation available in different programming languages.

Constructor overloading - if you create a shape class and then subclass circle and rectangle - you will have two constructors, one passing in radius and one passing in height and width.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900