Versions Compared

Key

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

...

Furthermore, methods or constructors with the same parameter types that differ only in their declaration order, are typically not flagged by Java compilers. This can result in errors if a developer does not consult the documentation at every use of the method or constructor. A related pitfall is to associate differing semantics with each of the overloaded methods or constructors. Defining different semantics sometimes necessitates different orderings of the same method parameters, creating a vicious circle. Consider for example a getDistance() method whose one overloading returns the distance traveled from the source while another (with rearranged parameters) returns the uncovered distance to the destination. An implementer may not realize the difference unless the documentation is consulted at every use.

Noncompliant Code Example (constructor)

Constructors cannot be overridden and can only be overloaded. This noncompliant code example shows the constructor Con with three overloadings Con(int, String), Con(String, int) and Con(Integer, String).

...

Failure to exercise caution while passing arguments to these constructors can create confusion as these overloadings can contain the same number of similarly typed parameters. Overloading must also be avoided when the overloaded constructors or methods provide inconsistent functionality for arguments of the same types, differing just in their declaration order.

Compliant Solution (constructor)

This compliant solution uses public static factory methods instead of public class constructors.

Code Block
bgColor#ccccff
public static void ConName1(int i, String s) { /* Initialization Sequence #1 */ }
public static void ConName2(String s, int i) { /* Initialization Sequence #2 */ }
public static void ConName3(Integer i, String s) { /* Initialization Sequence #3 */ }

Noncompliant Code Example (method)

In this noncompliant code example, the InformationLeak class holds a HashMap instance and returns a particular record to the caller based on either its key value in the map or the actual mapped value. The getData() method has been overloaded to return the contained data indexed by the key value in the former case. In the latter case, it checks whether a particular record exists before formatting and returning it as a String object. The InformationLeak class inherits from java.util.HashMap and overrides its get() method to provide the checking functionality. This implementation can be extremely confusing to the client who will expect the getData() methods to behave in a similar fashion and not depend on whether an index of the record is specified or the value to be retrieved.

...

Consequently, a client programmer may not realize that the wrong element has been removed from the list.

Compliant Solution (method)

Naming the two related methods differently eliminates overloading.

Code Block
bgColor#ccccff
public Integer getDataByIndex(int i) { /* no longer overloaded */ }

public String getDataByValue(Integer i) { /* no longer overloaded */ }

Risk Assessment

Ambiguous uses of overloading can lead to unexpected results.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

MET00-J

low

unlikely

high

P1

L3

Automated Detection

TODO

Related Vulnerabilities

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

Bibliography

Wiki Markup
\[[API 2006|AA. Bibliography#API 06]\] [Interface Collection|http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html]
\[[Bloch 2008|AA. Bibliography#Bloch 08]\] Item 41: Use overloading judiciously

...