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

Inheritance, Polymporhism, and Dynamic Dispatch (Java Language)

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();

My answer(s):

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

og

gc

gt

Answer:

Variable Static Dynamic
og object g
gc game c
gt game t

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 ; Answer: Yes. Static: Game Dynamic: Chess

Game gx = og; Answer: No. Static: Object Dynamic: Game

Game gx = (Game)og; Answer: Yes. Static: Object Dynamic: Game

Chess cx = (Chess)gt; Answer: Yes. Static: Chess Dynamic: ThreeD

ThreeDChess tx = (ThreeDChess)gc; Answer: No Static: Game Dynamic: Chess

C:

gc.players(); // Answer: Yes, 2 is printed, Since gc is refering object of Chess class.

og.players(); // Answer: No, Syntax error. og is referening Object class.

t.players(); // Answer: Yes, 2 is printed, Since t is refering the ThreeDChess class, Which is being calling its super class method

((ThreeDChess)gc).hasMoveableBoard(); // Answer: No. Would throw a exception java.lang.ClassCastException: Chess cannot be cast to ThreeDChess. Since gc referring Chess class only.

((ThreeDChess)gt).hasMoveableBoard(); // Answer: Yes, true is printed. hasMoveableBoard method of ThreeDChess class. Since gt is refering ThreeDChess class.

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!