当前位置: 技术问答>java相关
有关继承中方法覆盖时访问控制的问题?
来源: 互联网 发布时间:2015-10-17
本文导语: 如果父类的某个方法访问控制为protected,那么子类是否可以为public或private呢? 就是在覆盖中访问控制是可以扩大呢还是缩小呢? | if only override: Overriding a method means that its entire functiona...
如果父类的某个方法访问控制为protected,那么子类是否可以为public或private呢?
就是在覆盖中访问控制是可以扩大呢还是缩小呢?
就是在覆盖中访问控制是可以扩大呢还是缩小呢?
|
if only override:
Overriding a method means that its entire functionality is being replaced. Overriding is something done in a child class to a method defined in a parent class. To override a method a new method is defined in the child class with exactly the same signature as the one in the parent class. This has the effect of shadowing the method in the parent class and the functionality is no longer directly accessible.
Java provides an example of overriding in the case of the equals method that every class inherits from the granddady parent Object. The inherited version of equals simply compares where in memory the instance of the class references. This is often not what is wanted, particularly in the case of a String. For a string you would generally want to do a character by character comparison to see if the two strings are the same. To allow for this the version of equals that comes with String is an overriden version that performs this character by character comparison
Overriding a method means that its entire functionality is being replaced. Overriding is something done in a child class to a method defined in a parent class. To override a method a new method is defined in the child class with exactly the same signature as the one in the parent class. This has the effect of shadowing the method in the parent class and the functionality is no longer directly accessible.
Java provides an example of overriding in the case of the equals method that every class inherits from the granddady parent Object. The inherited version of equals simply compares where in memory the instance of the class references. This is often not what is wanted, particularly in the case of a String. For a string you would generally want to do a character by character comparison to see if the two strings are the same. To allow for this the version of equals that comes with String is an overriden version that performs this character by character comparison
|
不可扩大
|
能扩不能缩
|
自己动手试试就知道了
|
能缩不能扩
|
只能为保护类或者私有类,不可为Public