Friday, March 4, 2011

.NET interview questions :- What is the use of private constructor ?

.NET interview questions :- What is the use of private constructor ?


Answer:

Below is the video and text which explains everything






This is one of those .NET interview question which is asked from the perspective to test if you understand the importance of private constructors in a class.
When we declare a class constructor as private , we can not do 2 things:-

  • We can not create a object of the class.

  • We can not inherit the class.
Now the next question which the interviewer will ask , whats the use of such kind of class which can not be inherited neither instantiated.
Many times we do not want to create instances of certain classes like utility , common routine classes. Rather than calling as shown below

clsCommon common = new clsCommon();
common.CheckDate("1/1/2011");

You would like to call it as

clsCommon.CheckDate("1/1/2011");
Check my 21 important .NET interview questions and answers

1 comment:

James said...

It would also be nice to point out that you can create and instance of the class but from within a static function using the private constructor