What is a private constructor? Where will you use it?
Submitted by: AdministratorWhen you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.
If you declare a Constructor as private then it doesn't allow to create object for its derived class, i.e you loose inherent facility for that class.
Example:
Class A
{
// some code
Private Void A()
{
//Private Constructor
}
}
Class B:A
{
//code
}
B obj = new B();// will give Compilation Error
Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an 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.
Submitted by: Administrator
If you declare a Constructor as private then it doesn't allow to create object for its derived class, i.e you loose inherent facility for that class.
Example:
Class A
{
// some code
Private Void A()
{
//Private Constructor
}
}
Class B:A
{
//code
}
B obj = new B();// will give Compilation Error
Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an 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.
Submitted by: Administrator
Read Online OOP Job Interview Questions And Answers
Top OOP Questions
☺ | What is pure virtual function in OOP? |
☺ | When to use Interface over abstract class? |
☺ | Whats the Difference between Interface and Abstract Class? |
☺ | Can we call a base class method without creating instance? |
☺ | What is Abstract method? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |