package test;
import borrowLoan.Abc;
public class Text extends Abc{
public void printList(){
System.out.println("234");
}
public static void main(String[] args) {
Abc text = new Text();
text.printList();
}
}package borrowLoan;
public class Abc {
private int i=0;
public Abc(){
i=2;
}
public void printList(){
System.out.println("123");
}
}运行结果是234,书上讲,对象的实际类在运行时决定使用该方法的哪个实现,可见,这里它调用的是子类中的printList方法,但是当我把子类和父类中的printList方法的修饰符改成protected后,运行的结果却是告诉我,父类中的printList方法是不可见得,既然它调用的应该是子类中的printList方法,那又何来这个错误呢?纠结中(注意:这两个类在不同包中)
import borrowLoan.Abc;
public class Text extends Abc{
public void printList(){
System.out.println("234");
}
public static void main(String[] args) {
Abc text = new Text();
text.printList();
}
}package borrowLoan;
public class Abc {
private int i=0;
public Abc(){
i=2;
}
public void printList(){
System.out.println("123");
}
}运行结果是234,书上讲,对象的实际类在运行时决定使用该方法的哪个实现,可见,这里它调用的是子类中的printList方法,但是当我把子类和父类中的printList方法的修饰符改成protected后,运行的结果却是告诉我,父类中的printList方法是不可见得,既然它调用的应该是子类中的printList方法,那又何来这个错误呢?纠结中(注意:这两个类在不同包中)
如果是在同一个包中,依然调用的是子类的东西。