Question 4-2

Suppose we have the following two class definitions.

class A {
  public int f(int x) {
    return 2 * x;
  }
  public int g(int x) {
    return f(x * 3);
  }
}
class B extends A {
  public int f(int x) {
    return 5 * x;
  }
  public int h(int x) {
    return f(7 * x);
  }
}
And suppose we execute the following run method.
public static void run() {
  B b = new B();
  IO.println(b.f(1) + " " + b.g(1) + " " + b.h(1));
  A a = b;
  IO.println(a.f(1) + " " + a.g(1));
}
What would the method print?

Solution

5 15 35
5 15

Back to 8:00 Quiz 4