package 1;
public class Father{
protected Father(){
System.out.println("This is Father");
}
protected void print(){
System.out.println("This is print() from Father")
}
}
package 2;
import 1.Father;
public class Son extends Father{
Son(){
System.out.println("This is Son");
print();
Father father = new Father(); //Error:The constructor Father() is not visible
}
public static void main(String args[]){
Son son = new Son();
}
I understand that, since they are not in the same package, protected method can only be seen in an extending class(here Son), but why a protected method print() can be called while protected constructor can not?
EDIT:
I kind of get logic behind this. protected is seen either (1)in the same package or (2) child class in other package. Now when I call new Father(),error comes:
For (1) new Father() is not called in the same package. Error!
For (2) Since constructor is not inherited, there is no parent constructor existing in child class. Error!
So we can use super() to refer to the constructor in parent class, which is a fix method for case(2).(There is no way to fix (1) because I already put parent class and child class into 2 different packages!OK...There is one way, change protected to public, which is too obvious to see)