Hi
to all Ranchers here, i have a doubt in overloading and overriding method resolution , while going through questions i came across on this and the question goes like this ...
First question(Overloading) :
public class VarargsOverloading {
public void operation(
String str) {
String signature = "(String)";
out.println(str + " => " + signature);
}
public void operation(String str, int m) {
String signature = "(String, int)";
out.println(str + " => " + signature);
}
public void operation(String str, int m, int n) {
String signature = "(String, int, int)";
out.println(str + " => " + signature);
}
public void operation(String str, Integer... data) {
String signature = "(String, Integer[])";
out.println(str + " => " + signature);
}
public void operation(String str, Number... data) {
String signature = "(String, Number[])";
out.println(str + " => " + signature);
}
public void operation(String str, Object... data) {
String signature = "(String, Object[])";
out.println(str + " => " + signature);
}
public static void main(String[] args) {
VarargsOverloading ref = new VarargsOverloading();
ref.operation("1. (String)");
ref.operation("2. (String, int)", 10);
ref.operation("3. (String, Integer)", new Integer(10));
ref.operation("4. (String, int, byte)", 10, (byte) 20);
ref.operation("5. (String, int, int)", 10, 20);
ref.operation("6. (String, int, long)", 10, 20L);
ref.operation("7. (String, int, int, int)", 10, 20, 30);
ref.operation("8. (String, int, double)", 10, 20.0);
ref.operation("9. (String, int, String)", 10, "what?");
ref.operation("10.(String, boolean)", false);
}
}
Output of First Question :
1. (String) => (String)
2. (String, int) => (String, int)
3. (String, Integer) => (String, int)
4. (String, int, byte) => (String, int, int)
5. (String, int, int) => (String, int, int)
6. (String, int, long) => (String, Number[])
7. (String, int, int, int) => (String, Integer[])
8. (String, int, double) => (String, Number[])
9. (String, int, String) => (String, Object[])
10.(String, boolean) => (String, Object[])
Second Question(Overridding) :
import static java.lang.System.out;
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);
}
}
public class OneSubclass extends OneSuperclass {
//public int doIt(String str, Integer[] data) // Overridden a(1)
public int doIt(String str, Integer... data) // Overridden b(1)
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);
}
Output of Second Question : commenting Overridden a(1)
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[])
Output of second question : commenting // Overridden (b)
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[])
My doubt is here is in method overloading option4 that is
(String, int, byte) => (String, int, int) is resolved as byte is converted to more specific int(rather tahn anyother like float and double ) and mthod is resolved at complie time .
But in case of overriding same option4 that is(commenting Overridden a(1))
(String, int, byte) => (String, Number[]) is resolved , here isn't byte is converted to int (which is more specific than float or double) if so then why it's not calling (String, int, byte) => (String, Integer[]) instead.
and in all options that is commenting // Overridden (b) why methods with integer and bytes as are not calling (String, Integer[]) rather than calling (String,Number[]) form of the method.
Am too confused with this stuff from last 2 days , before asking any favour am feeling sorry to put so long coding , can anyone please explain me with clear details and if possible can give good links on this .