Discussion 5: Object-Oriented Programming

Solutions

Download Solution Code download
Exercise 1: Compilation Semantics
Consider the following gameSnippet() method:
1
2
3
4
5
6
/** Simulates the creation of a game instance with one human player. */
static void gameSnippet() {
  GameEngine engine = new GameEngine();
  Actor actor = new Player("Himmel", "human", engine);
  System.out.println("Created a " + actor.species());
}
1
2
3
4
5
6
/** Simulates the creation of a game instance with one human player. */
static void gameSnippet() {
  GameEngine engine = new GameEngine();
  Actor actor = new Player("Himmel", "human", engine);
  System.out.println("Created a " + actor.species());
}
What happens when we (try to) run this method? Explain your answer, referencing ideas from lecture.
This will not compile! The error is on line 5; the species() method is only defined for Player, not Actor. The compile-time reference rule says that the static type of a variable is used to determine which methods can be invoked on it. Since the species() method is not available to Actors (even though actor references a Player object), the compiler does not accept this code.
Exercise 2: Execution Semantics
Suppose that we execute the following simulate() method:
1
2
3
4
5
6
7
8
/** Simulates the first turn of a game instance with one player and one monster. */
static void simulate() {
  GameEngine engine = new GameEngine();
  Actor actor1 = new Player("Frieren", "elf", engine);
  Actor actor2 = new Monster("Demon King", engine);
  actor1.takeTurn();
  actor2.takeTurn();
}
1
2
3
4
5
6
7
8
/** Simulates the first turn of a game instance with one player and one monster. */
static void simulate() {
  GameEngine engine = new GameEngine();
  Actor actor1 = new Player("Frieren", "elf", engine);
  Actor actor2 = new Monster("Demon King", engine);
  actor1.takeTurn();
  actor2.takeTurn();
}
(a)
What is the static type of the variable actor1? What is the dynamic type of the object that it references?
The static type of actor1 is Actor, as this is the type that appears in its declaration. actor1 references an object with dynamic type Player since the Player constructor was invoked on the RHS of the assignment statement to actor1.
(b)
When line 6 executes, which method body do we enter? How does Java figure this out?
We enter the body of the Player.takeTurn() method. actor1 references an object with dynamic type Player, and dynamic dispatch dictates that this dynamic type is used to determine which "version" of the takeTurn() method is executed.
(c)

Draw a memory diagram that depicts the state of program immediately after entering the takeTurn() method on line 6 (i.e., before any of its local variables have been initialized). You can fill in any valid value for each Actor’s health and power.

The simulate() call frame should be the lowest one depicted on the runtime stack (since we don’t know how we got into this method). To simplify your diagram a bit, you can visualize the GameEngine as an empty rounded rectangle (omitting its fields). Draw all fields of any other objects.