I have a class A in package p1 that has a protected constructor. I have another class B in a different package p2 that is a subclass of class A. The requirement is to access the protected constructor of class A in some method of class B without changing the access modifier of the constructor in class A.
When I am trying to do the same, I am getting an error saying that the constructor is not visible.
package p1;
public class A
{
protected A()
{
System.out.println("Hello");
}
}
package p2;
import p1.A;
public class B extends p1.A
{
public static void main(String args[])
{
new A(); // Error is on this line
}
}
Note that I can access the constructor of class A within the constructor of class B using super() but the requirement is to access it within some function of class B without changing the access modifier of the constructor of class A.