Galera, estou estudando para fazer a certificação do Java 5 e encontrei o seguinte exemplo:
[code]
public class OneSuperclass {
public int doIt(String str, Integer… data) throws java.io.EOFException,
java.io.FileNotFoundException { // (1)
String signature = “(String, Integer[])”;
out.println(str + " => " + signature);
return 1;
}
public void doIt(String str, Number... data) { // (2)
String signature = "(String, Number[])";
out.println(str + " => " + signature);
}
}[/code]
import static java.lang.System.out;
public class OneSubclass extends OneSuperclass {
// public int doIt(String str, Integer[] data) // Overridden (a)
public int doIt(String str, Integer... data) // Overridden (b)
throws java.io.FileNotFoundException {
String signature = "(String, Integer[])";
out.println("Overridden: " + str + " => " + signature);
return 0;
}
public void doIt(String str, Object... data) { // Overloading
String signature = "(String, Object[])";
out.println(str + " => " + signature);
}
public static void main(String[] args) throws Exception {
OneSubclass ref = new OneSubclass();
ref.doIt("1. (String)");
ref.doIt("2. (String, int)", 10);
ref.doIt("3. (String, Integer)", new Integer(10));
ref.doIt("4. (String, int, byte)", 10, (byte) 20);
ref.doIt("5. (String, int, int)", 10, 20);
ref.doIt("6. (String, int, long)", 10, 20L);
ref.doIt("7. (String, int, int, int)", 10, 20, 30);
ref.doIt("8. (String, int, double)", 10, 20.0);
ref.doIt("9. (String, int, String)", 10, "what?");
ref.doIt("10.(String, boolean)", false);
}
}
Gerando a seguinte saída:
Overridden: 1. (String) => (String, Integer[])
Overridden: 2. (String, int) => (String, Integer[])
Overridden: 3. (String, Integer) => (String, Integer[])
4. (String, int, byte) => (String, Number[])
Overridden: 5. (String, int, int) => (String, Integer[])
6. (String, int, long) => (String, Number[])
Overridden: 7. (String, int, int, int) => (String, Integer[])
8. (String, int, double) => (String, Number[])
9. (String, int, String) => (String, Object[])
10.(String, boolean) => (String, Object[])
Consegui entender todas as saídas, exceto 1.
Alguém tem alguma explicação racional sobre a primeira saída!? Se eu encontrasse uma questão desta na prova tinha errado, pois marcaria Compilation Error .