What is a private constructor? where will you use it?

Answer Posted / ramkishore mateti

When you declare a Constructor with Private access modifier
then it is called Private Constructor.

If you declare a Constructor as private then it don't allow
to create object for its derived class,i.e you loose
inhirect facility for that class.
Ex:
Class A
{
// some code
Private Void A()
{
//Private Constructor
}
}

Class B:A
{
//code
}

B obj = new B();// will give Compilation Error

Because Class A construcor declared as private hence its
accessbility limit is to that class only ,Class B can't
access. As i explained the heirarchy of Constructors in the
previous qn, when we create a object for Class B that
constructor will call constructor A but class B have no
rights to access the Class A constructor hence we will get
compilation error.

Is This Answer Correct ?    29 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a method be sealed in c#?

480


What is the purpose of namespace?

462


Can a struct be null?

485


How to put assembly in gac?

534


What is console programming language?

474






Can I get the name of a type at runtime?

521


How many bytes is an int in c#?

499


What is callback function in c#?

521


Is c# different than c++?

506


What is the difference between string keyword and system.string class?

506


Why singleton class is sealed in c#?

503


What are the extension methods in c#?

487


What does it mean to override a method?

463


Explain lock, monitors, and mutex object in threading.

505


Can abstract class have constructor c#?

461