Static and runtime types: Correct Solution


Given the following type declarations:

public class Berthrep {
    public Berthrep prel(Berthrep a) {
        return a;
    }
}

public class Vadi extends Berthrep {
    public Berthrep prel(Berthrep a) {
        return this;
    }
}

public class Dapro extends Berthrep {
    public Berthrep prel(Berthrep a) {
        return new Dapro();
    }
}

...and given the following setup code:

Dapro foo = new Dapro();
Berthrep bar = new Vadi();

Part 1

Draw an AST for the following expression, labeling the static type (a.k.a. compile-time type) of each node in the tree:

foo.prel(foo.prel(bar))

(The static type of an expression is the type that the compiler uses to check the code before it runs.)

Solution


Part 2

Draw an AST for the same expression, this time labeling the runtime type of each node in the tree.

(The runtime type of an expression is the type of the actual value that appears when the code runs.)

Solution


Related puzzles: