...
The difference between these two is that the overridden method gets invoked from the subclass while the hidden method may get invoked from either the superclass or the subclass, depending on how it is invoked (name qualification).
Noncompliant Code Example
To differentiate between overriding and hiding, a common nomenclature is used. The method to be invoked is decided at either compile time (if the base method is static
, as in this noncompliant example) according to the type of the qualifier or at run time otherwise (for non-static methods). A qualifier is a part of the invocation expression before the dot (for example, admin
and user
here).
This noncompliant example attempts to override a static method but fails to consider it as a hiding case. As a result the displayAccountStatus
method of the superclass gets invoked on both the calls. Moreover, expressions that are normally used for dynamic dispatch while overriding have been used even though achieving this is impossible with static
methods.
...
This compliant solution correctly classified classifies this case as hiding and uses absolute class names GrantAccess
and GrantUserAccess
to clearly state the intent. Refrain from qualifying a static
method invocation with an expression meant for dynamic dispatch.
...
Wiki Markup |
---|
Technically, a {{private}} method cannot be hidden or overridden. There is no requirement that {{private}} methods with the same signature in the subclass and the superclass bear any relationship in terms of having the same return type or {{throws}} clause, the necessary conditions for _hiding_. \[[JLS 05|AA. Java References#JLS 05]\]. Consequently, there may be no _hiding_ when the methods have different return types or {{throws}} clauses. |
Risk Assessment
Confusing overriding and hiding can produce unexpected results.
...