Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
When a class declares a static method _m_, the declaration of _m_ hides any method _m'_, where the signature of _m_ is a subsignature of the signature of _m'_ and the declaration of _m'_ is both in the superclasses and superinterfaces of the declaring class and also would otherwise be accessible to code in the declaring class \[[JLS 2005|AA. Bibliography#JLS 05]\] [" (_Java Language Specification_, [§8.4.8.2 "Hiding (by Class Methods)"|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.2]\[[JLS 2005|AA. Bibliography#JLS 05]\]).

An instance method defined in a subclass overrides another instance method in the superclass when both have the same name, number and type of parameters, and return type.

Hiding and overriding differ in the determination of which method is invoked from a call site. For overriding, the method invoked is determined at runtime based on the basis of the specific object instance in hand. For hiding, the method invoked is determined at compile time based on the basis of the specific qualified name or method invocation expression used at the call site. Although the Java language provides unambiguous rules for determining which method is invoked, the results of these rules are often unexpected. Additionally, programmers sometimes expect method overriding in cases where the language provides method hiding. Consequently, programs must never declare a class method that hides a method declared in a superclass or superinterface.

Noncompliant Code Example

In this noncompliant example, the programmer hides the static method instead of rather than overriding it. Consequently, the code invokes the displayAccountStatus() method of the superclass at two different call sites instead of invoking the superclass method at one call site and the subclass method at the other.

...

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 2005|AA. Bibliography#JLS 05]\]. Consequently, hiding cannot occur when theprivate methods have different return types or {{throws}} clauses.

...

MET07-EX0: Occasionally an API provides hidden methods. Invoking those methods is not a violation of this rule, provided that all invocations of hidden methods use qualified names or method invocation expressions that explicitly indicate which specific method is invoked. If the displayAccountStatus() is were a hidden method, for example, the following implementation of the choose() method is would be an acceptable alternative:

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e6edc614024ebe0e-66d392d9-4bd949e4-9458b6d6-6681824fc4dc2175146552c4"><ac:plain-text-body><![CDATA[

[[Bloch 2005

AA. Bibliography#Bloch 05]]

Puzzle 48: . All I Get Is Static get is static

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0f8831851e62c820-98602226-45134ab1-9a77b668-a7d9f7659e8a3de70d618619"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

["§8.4.8.2, Hiding (by Class Methods)"

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.2]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0b439c293b454b7a-9aaba348-40944dcd-9a0f942d-4800e7f45b2934d8d79f2d59"><ac:plain-text-body><![CDATA[

[[Tutorials 2008

AA. Bibliography#Tutorials 08]]

[Overriding and Hiding Methods

http://java.sun.com/docs/books/tutorial/java/IandI/override.html]

]]></ac:plain-text-body></ac:structured-macro>

...