When attempting to create an object of the parent class, which has a protected constructor, I tried using a child class reference and was able to create the object successfully. However, when using a parent class reference, it resulted in a compilation error.
When attempting to create an object of the parent class, which has a protected constructor, I tried using a child class reference and was able to create the object successfully. However, when using a parent class reference, it resulted in a compilation error.
This is the code used:
package secondPackage;
public class AccessPra {
protected AccessPra() {
System.out.println("protected constructor");
}
}
package firstPackage;
import secondPackage.*;
public class MainClass extends AccessPra {
public static void main(String[] args) {
AccessPra obj1 = new AccessPra(); // Throwing Error
MainClass obj2 = new MainClass(); // This line is working
}
}