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();
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started