Question 2-6

Consider the following program.

class Ident {
    int f(int x) { return x; }
    int g(int x) { return f(f(x)); }
}

class Square extends Ident {
    int f(int x) { return x * x; }
}

class Main {
    public static void main(String[] args) {
        Ident a = new Ident();
        Ident b = new Square();
        Square c = new Square();
        System.out.println(a.g(3) + " " + b.g(3) + " " + c.g(3));
    }
}
What does this program print?

Solution

3 81 81

UML and patterns

Back to Review for Quiz 2