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]\] ["8.4.8.2 Hiding (by Class Methods)"|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.2]. |
Programmers may confuse hiding with overriding resulting in unexpected behavior. 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 the 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 specific object instance in hand. For hiding, the method invoked is determined at compile time based on the specific qualified name or method invocation expression used at the call site.
...
In this noncompliant example, the programmer hides the static method instead of overriding it. Consequently, the code unexpectedly invokes the displayAccountStatus()
method of the superclass at two different call sites , rather than instead of invoking the superclass method at one call site and the subclass method at the other as the programmer likely intended.
Code Block | ||
---|---|---|
| ||
class GrantAccess { public static void displayAccountStatus() { System.out.print("Account details for admin: XX"); } } class GrantUserAccess extends GrantAccess { public static 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"); } } |
...
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"); } } |
Wiki Markup |
---|
YouThe canmethods alsoinherited overloadfrom the methodssuperclass inheritedcan fromalso thebe superclassoverloaded in a subclass. Overloaded methods are new methods, unique to the subclass and neither hide nor override the superclass method \[[Tutorials 2008|AA. Bibliography#Tutorials 08]\]. |
...
Automated detection of violations of this guideline are is straightforward. Automated determination of cases where method hiding was is unavoidable appears to be is infeasible. However, determining whether all invocations of hiding or hidden methods explicitly indicate which specific method is invoked is straightforward.
...