Consider the contents of following two files:
//File A.java
package a;
public class A
{
A(){ }
public void print(){ System.out.println("A"); }
}
//File B.java
package b;
import a.*;
public class B extends A
{
B(){ }
public void print(){ System.out.println("B"); }
public static void main(
String[] args)
{
new B();
}
}
What will be printed when you try to compile and run class B?
a) it will print A
b) it will print B
c) it will not compile
the correct ans is c and this is the explanation
Note that there is no modifier for A's constructor. So it has default access. This means only classes in package a can use it. Also note that class B is in a different package and is extending from A. In B's constructor the compiler will automatically add super() as the first line. But as A() is not accessible in B, this code will not compile.
Even though A and B are not in the same package A is public why does the acces modifier to the constructor have to be public i thought since class A is public it is automatically accesible from anywhere