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