Hello,
I studied the OO concepts regarding method overloading vs. overriding.
Can someone explain why example 1 compiles, by 2 does not?
1)
class A {
int m(int i) { return 1;};
int m(float f) { return 1; } //overloads m(int i)
}
class B extends A {
void a() { m(1); m(5f); } // calls overloaded methods
}
2)
class A {
int m(int i) { return 1;};
}
class B extends A {
int m(float f) { return 1; } // overloads method from super
void a() { m(1); m(5f); }
}
The second example gives me the following error:
interface1.java:45: reference to m is ambiguous, both method m(int) in A and met
hod m(float) in B match
void a() { m(1); m(5f); }
^
1 error
According to Khalid: "A method can be overloaded in the class it is defined in, or in a subclass of its class"
I am sure I followed all the rules for method overloading vs. overriding.
If I change the parameter m(float f) into m(
String s) and the calls in class B accordingly, it works fine in both cases.
Any ideas?
Thanks,
Bernd