I am wondering why the call of
z.f(-6);
in class M refers to the following function in class B:
public void f(double y) {
this.x = (int) y + B.y;
}
instead of using function f in class A, since b.x is covered by A. Or rather uses
public void f (int y) {
this.x = y*2;
B.y = this.x;
}
in class B where at least the parameter type matches.
Complete Code below:
public class A {
public int x = 1;
public A(int x) {
this.x += x;
}
public A (double x) {
x += x;
}
public void f(double x) {
this.x = this.x + (int) (x + B.y);
}
}
public class B extends A {
public static int y = 3;
public int x = 0;
public B (double x) {
super((int) x);
}
public void f(int y) {
this.x = y*2;
B.y = this.x;
}
public void f(double y) {
this.x = (int) y + B.y;
}
}
public class M {
public static void main (String[] args){
A a = new A(B.y);
a.f(1);
B b = new B(3.0);
A z = b;
z.f(-5.0);
z.f(-6);
System.out.println(b.x + " " + z.x);
}
}