java访问修饰符和重写方法

编程入门 行业动态 更新时间:2024-10-25 20:22:54
本文介绍了java访问修饰符和重写方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么Java指定覆盖方法的访问说明符可以允许比重写方法更多但不是更少的访问?例如,超类中的受保护实例方法可以在子类中公开,但不是私有的。

Why does Java specify that the access specifier for an overriding method can allow more, but not less, access than the overridden method? For example, a protected instance method in the superclass can be made public, but not private, in the subclass.

推荐答案

这是一个OOP中的基本原则:子类是父类的完全成熟实例,因此必须至少提供与父类相同的接口。使受保护/公共事物不那么明显会违反这一想法;你可以让子类无法用作父类的实例。

It's a fundamental principle in OOP: the child class is a fully-fledged instance of the parent class, and must therefore present at least the same interface as the parent class. Making protected/public things less visible would violate this idea; you could make child classes unusable as instances of the parent class.

更多推荐

java访问修饰符和重写方法

本文发布于:2023-11-12 18:58:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   方法   修饰符   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!