Java Reference
In-Depth Information
Bibliography
[API 2013]
Class Pattern, “Backslashes, Escapes, and Quoting”
Package java.sql
[JLS 2013]
§3.10.6, “Escape Sequences for Character and String Literals”
72. Do not use overloaded methods to differentiate between runtime
types
Java supports overloading methods and can distinguish between methods with different
signatures. Consequently, with some qualifications, methods within a class can have the
samenameiftheyhavedifferentparameter lists.Inmethodoverloading,themethodtobe
invoked at runtime is determined at compile time. Consequently, the overloaded method
associated with the static type of the object is invoked even when the runtime type differs
for each invocation.
For program understandability, do not introduce ambiguity while overloading (see
Guideline 65 , Avoid ambiguous or confusing uses of overloading ), and use overloaded
methods sparingly [Tutorials 2013].
Noncompliant Code Example
This noncompliant code example attempts to use the overloaded display() method to
perform different actions depending on whether the method is passed an ArrayList<In-
teger> or a LinkedList<String> :
Click here to view code image
public class Overloader {
private static String display(ArrayList<Integer> arrayList) {
return "ArrayList";
}
private static String display(LinkedList<String> linkedList) {
return "LinkedList";
}
private static String display(List<?> list) {
return "List is not recognized";
}
Search WWH ::




Custom Search