I understand that the runtime type of an object is used when accessing methods, as is true when the following code is run (the result is "2"):
Now when we change the input parameter on the super class to one that does not exactly match the argument being sent in, the super class method is invoked and the result is "1":
class FWD {
int doMud(long x) { return 1; }
}
class Subaru extends FWD {
int doMud(int y) { return 2; }
}
class Race {
public static void main(String [] args) {
FWD f = new Subaru();
System.out.println(f.doMud(7));
}
}
I am obviously missing something here as I do not understand why the JVM does not continue to use the subclass method which defines an input parameter type that exactly matches the type coming int (int to int). Why is it that when the parameter type on the super class changes to a long that the JVM decides to use the super class method?
Thanks again for your help
g
Now when we change the input parameter on the super class to one that does not exactly match the argument being sent in, the super class method is invoked and the result is "1":
class FWD {
int doMud(long x) { return 1; }
}
class Subaru extends FWD {
int doMud(int y) { return 2; }
}
class Race {
public static void main(String [] args) {
FWD f = new Subaru();
System.out.println(f.doMud(7));
}
}
I am obviously missing something here as I do not understand why the JVM does not continue to use the subclass method which defines an input parameter type that exactly matches the type coming int (int to int). Why is it that when the parameter type on the super class changes to a long that the JVM decides to use the super class method?
Thanks again for your help
g