Versions Compared

Key

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

...

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.

Noncompliant Code Example

In this noncompliant example, the programmer hides the static method instead of 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.

Code Block
bgColor#FFCCCC
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.

...

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 the methods have different return types or {{throws}} clauses.

Exceptions

MET11-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 a hidden method, for example, the following implementation of the choose() method is an acceptable alternative:

Code Block
bgColor#ccccff
  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

Remediation Cost

Priority

Level

MET11-J

low

unlikely

medium

P2

L3

Automated Detection

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.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7b817418bdd5aa74-9eaa4f97-404d4358-8af7bc8f-dad1d66ca21431df2784b145"><ac:plain-text-body><![CDATA[

[[Bloch 2005

AA. Bibliography#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="ee2ee7a43bb76acc-887cd6e0-4fbd4609-b427972f-070951c51f7ad6f57ebbe5e5"><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="5511e8fffc3bfe98-79825227-4010404e-8944849e-c1a07261aef468f3baae64eb"><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>

...