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 (_Java Language Specification_, [§8, §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. References#JLS 05]\]). Wiki Markup
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.
...
Code Block | ||
---|---|---|
| ||
class GrantAccess { public void displayAccountStatus() { System.out.print("Account details for admin: XX"); } } class GrantUserAccess extends GrantAccess { @Override public void displayAccountStatus() { System.out.print("Account details for user: XX"); } } public class StatMethod { public static void choose(String username) { GrantAccess admin = new GrantAccess(); GrantAccess user = new GrantUserAccess(); if (username.equals("admin")) { admin.displayAccountStatus(); } else { user.displayAccountStatus(); } } public static void main(String[] args) { choose("user"); } } |
...
The methods inherited from the superclass can also be overloaded in a subclass. Overloaded methods are new methods unique to the subclass and neither hide nor override the superclass method \ [[Tutorials 2008|AA. References#Tutorials 08]\].unmigrated-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. References#JLS 05]\]. Consequently, hiding cannot occur when private methods have different return types or {{throws
}} clauses.
Exceptions
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()
were a hidden method, for example, the following implementation of the choose()
method would be an acceptable alternative:
...
Automated detection of violations of this rule is straightforward. Automated determination of cases where method hiding is unavoidable is infeasible. However, determining whether all invocations of hiding or hidden methods explicitly indicate which specific method is invoked is straightforward.
Bibliography
...
[[Bloch 2005AA. References#Bloch 05] ] | Puzzle 48. All I 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="a1c11a9f-6ad8-41a0-aef7-e0986c47af0e"><ac:plain-text-body><![CDATA[ | |
[[JLS 2005AA. References#JLS 05] ] | 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="2122ab0e-0059-4119-8a27-72e9ab401c8a"><ac:plain-text-body><![CDATA [ [[Tutorials 2008AA. References#Tutorials 08]] | http://java.sun.com/docs/books/tutorial/java/IandI/override.html] | ]]></ac:plain-text-body></ac:structured-macro> |
...