被子类所覆写的方法不能拥有比父类更严格的访问控制权限,目前接触到的三种访问权限大小关系为private<default<public
范例:观察private权限下的方法覆写
package com.lxh.eightchapter;
public class e168 {
private void content() {
System.out.println("父类");
}
}
package com.lxh.eightchapter;
public class e16802 extends e168{
public void content(){
System.out.println("子类");
}
}
package com.lxh.eightchapter;
public class e {
public static void main(String[] args) {
e16802 s=new e16802();
s.content();
}
}
子类
这样只是定义了一个新的方法而已
package com.lxh.eightchapter;
public class e168 {
private void content() {
System.out.println("父类");
}
public void hdd() {
this.content();
}
}
package com.lxh.eightchapter;
public class e16802 extends e168 {
public void content(){
System.out.println("子类");
}
}
package com.lxh.eightchapter;
public class e {
public static void main(String[] args) {
e16802 s=new e16802();
s.hdd();
}
}
父类
本程序从覆写要求来看,子类结构属于覆写,但是由于父类中content()方法使用了private定义,所以此方法将无法进行覆写。当子类实例化对象调用hdd()方法时,调用的并不是覆写的方法,如果成功覆写,调用的一定是子类中的content方法,所以private权限声明的方法无法被子类覆写。