Question 4-4

Consider the following two class definitions.

class X {
  public double g(double x) {
    return 3.0 * f(x);
  }
  public double f(double x) {
    return x;
  }
}
class Y extends X {
  public double f(double x) {
    return 5.0 * x;
  }
}
What will the following sequence of statements print?
Y y = new Y();
IO.println(y.f(2.0));
X x = y;
IO.println(x.f(2.0));
IO.println(x.g(2.0));

Solution


10.0
10.0
30.0

Back to Review for Quiz 4