是,超类的受保护方法可以被子类覆盖。如果超类方法受保护,则子类重写方法可以具有受保护或公共(但不能具有默认或私有) ) 这意味着子类重写的方法不能有较弱的访问说明符。
class A { protected<strong> </strong>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
以上就是我们能在Java中覆盖一个受保护的方法吗?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号