Java Reference
In-Depth Information
Applicability
Failingtotakeintoaccountallpossibilitieswithinalogicstatementcanleadtoacorrupted
running state, potentially resulting in unintentional information disclosure or abnormal
program termination.
Bibliography
[Hatton 1995]
§2.7.2, “Errors of Omission and Addition”
[Viega 2005]
§5.2.17, “Failure to Account for Default Case in Switch”
[Zadegan 2009]
A Lesson on Infinite Loops
65. Avoid ambiguous or confusing uses of overloading
Method and constructor overloading allows declaration of methods or constructors with
the same name but with different parameter lists. The compiler inspects each call to an
overloaded method or constructor and uses the declared types of the method parameters
to decide which method to invoke. In some cases, however, confusion may arise because
of the presence of relatively new language features such as autoboxing and generics.
Furthermore, methods or constructors with the same parameter types that differ only
in their declaration order are typically not flagged by Java compilers. Errors can result
when a developer fails to consult the documentation at each use of a method or construct-
or. A related pitfall is to associate different semantics with each of the overloaded meth-
ods or constructors. Defining different semantics sometimes necessitates different order-
ings of the same method parameters, creating a vicious circle. Consider, for example, an
overloaded getDistance() method in which one overloaded method returns the distance
traveled from the source while another (with reordered parameters) returns the remain-
ing distance to the destination. Implementers may fail to realize the difference unless they
consult the documentation at each use.
Noncompliant Code Example (Constructor)
Constructors cannot be overridden and can only be overloaded. This noncompliant code
example shows the class Con with three overloaded constructors:
Click here to view code image
Search WWH ::




Custom Search