有如下两个类
Father 类:
public class Father {
public void f() {
System.out.println("Father.f()");
}
}
Son类:
public class Son extends Father{
private final void f() { //这里会出现编译错误 Cannot reduce the visibility of the inherited method from Father
System.out.println("Son.f()");
}
void test() {
f();
}
public static void main(String[] a) {
new Son().test();
}
}
出现上面错误的原因是因为如果子类要覆盖了父类中定义的方法,那么不能降低其可见性
所以修改子类的修改符即可