Can We have a Private Constructor in Class
Points To Remember
  • A constructor have the same name as that of a class.
  • A constructor does not have any return type, not even void.
  • Constructor of a class is called first, before any other method is called.
  • Constructor of a super class is called before the base class constructor. 
  • We can have a private constructor in a java class.
  • A class with a private constructor can not be inherited.
  • We cannot create object of a class with a private constructor.
What happens if we have a Private Constructor in a class.
Suppose if we make a private constructor of our class and try to create the object of that class from that class only.
In the example below, we have a class A and we created the constructor of this class that just prints a statement. Now we created the object of this class from a main method. The code will compile and run successfully to give a output. Thus, this proofs that we can have a private constructor in a class.

class A 
{

  private A(){
    System.out.println("Private Constructor of A");
  }

  public static void main(String args[]){
    System.out.println("Hello World");
    A obj = new A();

  }
}
Hello World
Private Constructor of A
What happens if we try to create an object of a class with  a Private Constructor.
Now, if we have a class with a private constructor and we try to create the object of this class e.g. If we have a class A and another class B, both with private constructors and we try to create object of class B from class A. This will throw a compile time exception stating that we can not create the object of class B because we cannot access its default constructor. Thus we cannot create the object of a class that has a private constructor.  Remember that, a whenever an object of a class is created, its default constructor must be accessible by the class that is trying to create its object.
class B{

  private B(){
    System.out.println("Private Constructor of B");
  }

}

class A 
{

  private A(){
    System.out.println("Private Constructor of A");
  }

  public static void main(String args[]){
    System.out.println("Hello World");
    A objectA = new A();
    B objectB = new B();

  }
}

A.java:19: error: B() has private access in B
    B objectB = new B();
                ^
1 error

What happens if we try to inherit a class with a Private Constructor.
Now, if we create a class B with a private constructor and try to inherit this class, we will get a compile time error stating that the class B have a private constructor. This means that a class with the private constructor can not be inherited.

class B{ private B(){ System.out.println("Private Constructor of B"); } } class A extends B { private A(){ System.out.println("Private Constructor of A"); } public static void main(String args[]){ System.out.println("Hello World"); } }
A.java:12: error: B() has private access in B
  private A(){
             ^
1 error
Thus we cannot extend a class or make an object of a class that has a private constructor because its default constructor becomes inaccessible. When we inherit a class the default constructor of the super class is executed before the constructor of the base class is executed. Also when we make an object of a class its default constructor is accessed before any other method is accessed. Thus constructor must be accessible for making the object of a class or inheriting a class. We can that a class with a private constructor can not be inherited nor we can make the object of this class.

No comments :

Post a Comment