Suppose there is a class A. Which of the following two access modifiers is a default one for a constructor?
public A()
{
private A()
{
//some code....
}
protected A()
{
//some code....
}
}
Suppose there is a class A. Which of the following two access modifiers is a default one for a constructor?
public A()
{
private A()
{
//some code....
}
protected A()
{
//some code....
}
}
It means the exact same thing as modifiers to functions and variables, only now it refers to who can CONSTRUCT an instance of the class.
public
- any one can call the constructor from anywhere in the code.
private
- Unable to construct from outside the class - typically used to enable control over who gets to instanciate the class with the use of a static member factory method. A good example of an appication found here
protected
- Like private
but now inheritance is involved - any subclass factory method can be used because now they can call this constructor.
As @dasblinkenlight mentions, if you do not specify any modifier, then they default to being package-private, meaning they are only visible to classes within the package.