Hi,
I am reading the
SCJP Tiger Study Guide, I don't quite follow how the overriding with varargs works on the example given. Please help me out.
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);
}
}
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);
}
}
It says that with overriden(a), the output will be the following:
1. (String) => (String, Number[])
2. (String, int) => (String, Number[])
3. (String, Integer) => (String, Number[])
4. (String, int, byte) => (String, Number[])
5. (String, int, int) => (String, Number[])
6. (String, int, long) => (String, Number[])
7. (String, int, int, int) => (String, Number[])
8. (String, int, double) => (String, Number[])
9. (String, int, String) => (String, Object[])
10.(String, boolean) => (String, Object[])
But with overriden (b), the output is:
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[])
My doubt is:
For output statment 1, 2, 3, 5, 7, When overriden (a) is used, why the OneSuperclass.doIt(String str, Number... data) operation is chosen? Why not OneSubclass.doIt(String str, Integer... data)? Isn't the signature of OneSubclass.doIt(String str, Integer... data) is more matching with the input parameter?
Thanks!
Lyn
[ October 02, 2005: Message edited by: Lyn Yang ]