Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java中是否可以覆盖(override)一个private方法 #10

Open
yebuguntang opened this issue Feb 7, 2022 · 0 comments
Open

Java中是否可以覆盖(override)一个private方法 #10

yebuguntang opened this issue Feb 7, 2022 · 0 comments

Comments

@yebuguntang
Copy link

Java中是否可以覆盖(override)一个private方法

答:不能覆盖一个private方法。

定义来分析:
查阅了很多资料,查到最好的定义是:重写(覆盖/覆写/override)是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。

继承层面分析:
重写的前提是继承,继承只能继承非私有的属性和方法(但可以通过暴露的公共方法访问私有属性和方法)。子类对父类的私有方法并不可见。所以可见方法只是同名并不是重写。
可以在方法上面加上注解@OverRide进行验证,报错信息如下:Method does not override method from its superclass。

写的很啰嗦,希望有大佬可以给出更简洁的答案。
对具体有关重写父类方法的规则感兴趣的可以读下这篇博客https://blog.csdn.net/heshuangyuan123/article/details/38896329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant