Question 1-9

Consider the following two class definitions.

class X {
  public double g(double x) {
    return f(x) * f(x);
  }
  public double f(double x) {
    return x + 1.0;
  }
}

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

Solution

4.0
4.0
16.0

Back to Review for Midterm 1