what is the use of private constructor?

 Posted by Bsrkvarma on 8/28/2014 | Category: OOPS Interview questions | Views: 1779 | Points: 40
Answer:

When we declare a class constructor as private , we can not do 2 things:-

-- We can not create a object of the class from out side of the class.
-- We can not inherit the class.

Private constructor is used for controlling the object generation which Is singleton design pattern implementation..

public class Singleton

{
private static Singleton instance = new Singleton();
//Private constructor, which does not allow object creation
//from outside the classs..
private Singleton() { }

public static Singleton GetInstance
{
get
{
return instance;
}
}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response

More Interview Questions by Bsrkvarma