Thursday, December 12, 2013

Published 12/12/2013 by with 0 comment

The constructor ... is not visible

When you invoke the private constructor of a class in another class, you’ll get the error message “The constructor ... is not visible”. And when the private constructor of a super class is invoked from the subclass implicitly or explicitly, you’ll get the following:

Implicit super constructor ... is not visible for default constructor. Must define an explicit constructor.
Implicit super constructor ... is not visible. Must explicitly invoke another constructor.

See an example below:

public class SuperClass
{

       private SuperClass()
       {
              System.out.println("Super class private constructor");
       }
      
       public SuperClass(int n)
       {
              System.out.println("Super class protected constructor");
       }
}

In the above code, the class has two constructors. Access modifier for one is private and another is public. So if you create an instance of this class using the private constructor in another class, you’ll encounter the error.

You cannot write SuperClass  superClassObject = new SuperClass();
You should call other constructors that you can access as per access modifier rule or the other way of instantiating the class (if available in that class). Sometimes such class has some getInstance() like methods.

Again, when you inherit a class that has private default constructor, you have to write your own constructor and invoke a constructor (that is accessible) within your constructor. In such case, if you do not write any constructor in the subclass you’ll get “Implicit super constructor ... is not visible for default constructor. Must define an explicit constructor.”

This happens because when you do not define any constructor, a default constructor is automatically created by the Java compiler and the default constructor invokes the default constructor of the super class. This is how the constructor is invoked implicitly.

See the code below:
//Code1
public class Subclass extends SuperClass
{
      
}
Or
//Code2
public class Subclass extends SuperClass
{

       public Subclass()
       {
             
       }
}

I have not defined any constructor in code1. In code2, I have defined a constructor but have not invoked any constructor explicitly. So the super class default constructor will implicitly be called.

In the code below I have defined a constructor. But still I called the super class default constructor explicitly through “super()”. I’ll encounter the error.

public class Subclass extends SuperClass
{

       public Subclass()
       {
              super();
       }
}

Now the solutions are:
1.      
      We cannot call the private constructor of class from the subclass or other class. You should look the other constructors or the other way of instantiating class.

2.       If the default constructor of a super class is private, you have to write your own constructor in the subclass and explicitly call visible constructors.

For the above scenario, correct coding would be:

public class Subclass extends SuperClass
{

       public Subclass()
       {
              super(10);
       }

}
    email this       edit

0 comments:

Post a Comment