Question: Inheritance, Polymporhism, and Dynamic Dispatch Please answer the following questions about type conformance and dynamic binding. Type your answers in below. Label each answer with

Inheritance, Polymporhism, and Dynamic Dispatch

Please answer the following questions about type conformance and dynamic binding. Type your answers in below. Label each answer with both the main category (A, B, C) and then the specific question (1, 2, 3).

Here are some class definitions:

public class Game{

public void players() { System.out.println(1 or more); }

} public class Chess extends Game {

public void boardSize() { System.out.println(8 by 8); } public void players() { System.out.println(2); }

} public class ThreeDChess extends Chess{

public void boardSize() {System.out.println(7 levels); } public void hasMoveableBoard() { System.out.println(true); }

}

Assume that these statements have already executed:

Game g = new Game(); Chess c = new Chess(); ThreeDChess t = new ThreeDChess(); Object og = g; Game gc = c; Game gt = t; // Add new statements here ONE AT A TIME

A) What are the static and dynamic types for the variables

og

gc

gt

B) Explain what would happen if each of the following statements were inserted ONE AT A TIME in the spot marked above. Answer the following questions for each statement:

- Will the statement compile? - If yes, what are the static and dynamic types of the variable declared?

Game gx = gc;

Game gx = og;

Game gx = (Game)og;

Chess cx = (Chess)gt;

ThreeDChess tx = (ThreeDChess)gc;

C) Finally, explain what would happen if each of the following statements were inserted ONE AT A TIME in the spot marked above. Answer the following questions for each statement:

- Will the statement compile?

- If yes, would the statement throw a ClassCastException?

- If no, then explain what output is shown

gc.players();

og.players();

t.players();

((ThreeDChess)gc).hasMoveableBoard();

((ThreeDChess)gt).hasMoveableBoard();

PreviousNext

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!