 
                            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.
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 code 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, causing it to print  Account details for admin despite being instructed to choose user rather than admin.
| Code Block | ||
|---|---|---|
| 
 | ||
| 
class GrantAccess {
  public static void displayAccountStatus() {
    System.out.println("Account details for admin: XX");
  }
}
class GrantUserAccess extends GrantAccess {
  public static void displayAccountStatus() {
    System.out.println("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");
  }
}
 | 
Compliant Solution
In this compliant solution, the programmer declares the displayAccountStatus() methods as instance methods , by removing the static keyword. Consequently, the dynamic dispatch at the call sites produces the expected result. The @Override annotation indicates intentional overriding of the parent method.
| 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 
Wiki Markup throws}}  clause,   the   necessary   conditions   for   hiding  \ [[JLS 2005|AA. Bibliography#JLS 05]\]. Consequently, hiding cannot occur when the methods have different return types or {{throws}} JLS 2015]. Consequently, hiding cannot occur when private methods have different return types or throws clauses.
Exceptions
MET11MET07-J-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:
| Code Block | ||
|---|---|---|
| 
 | ||
| 
  public static void choose(String username) {
    if (username.equals("admin")) {
      GrantAccess.displayAccountStatus();
    } else {
      GrantUserAccess.displayAccountStatus();
    }
  }
 | 
Risk Assessment
Confusing overriding and hiding can produce unexpected results.
| Rule | Severity | Likelihood | Detectable | 
|---|
| Repairable | Priority | Level | 
|---|
| MET07-J | Low | 
| Unlikely | 
| Yes | 
| No | P2 | L3 | 
Automated Detection
Automated detection of violations of this rule is straightforward. Automated determination of cases where in which 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.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
| Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Parasoft Jtest | 
 | CERT.MET07.AHSM | Do not hide inherited "static" member methods | 
Bibliography
| Puzzle 48, " | 
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bb8f7f82-d7e4-4d59-8ed2-c7eadcb11938"><ac:plain-text-body><![CDATA[
[[Bloch 2005
AA. Bibliography#Bloch 05]]
| 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="adcdc79c-7302-4cfb-b2b7-f817726e508d"><ac:plain-text-body><![CDATA[
[[JLS 2005
AA. Bibliography#JLS 05]]
| [JLS 2015] | 
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="e860cd9e-52ed-47b2-b5aa-6aebefb01e11"><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>
...
05. Methods (MET) MET12-J. Ensure objects that are equated are equatable