java - Why can I override a protected method with public method? -
the java compiler doesn't complain when override protected method public method. what's happening here? overriding or hiding parent method since parent method has lower visibility?
a sub-class can widen access modifier, because still valid substitution super-class. java specification requirements in overriding , hiding:
the access modifier (§6.6) of overriding or hiding method must provide @ least access overridden or hidden method, follows:
- if overridden or hidden method public, overriding or hiding method must public; otherwise, compile-time error occurs.
- if overridden or hidden method protected, overriding or hiding method must protected or public; otherwise, compile-time error occurs.
- if overridden or hidden method has default (package) access, overriding or hiding method must not private; otherwise, compile-time error occurs.
Comments
Post a Comment