我们可以覆盖Java中受保护的方法吗?
是的,超类的受保护方法可以被子类覆盖 。如果超类方法受到保护,则子类重写方法可以具有保护 或公共 (但不是默认 或私有),这意味着子类重写方法不能具有较弱的访问说明符。
示例
class A {
protected void protectedMethod() {
System.out.println("superclass protected method");
}
}
class B extends A {
protected void protectedMethod() {
System.out.println("subclass protected method");
}
}
public class Test {
public static void main(String args[]) {
B b = new B();
b.protectedMethod();
}
}输出结果
subclass protected method
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短