Inheritance, Polymporhism, and Dynamic Dispatch (Java Language) Please answer th
ID: 3868346 • Letter: I
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 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:
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.
Variable Static Dynamic og object g gc game c gt game tExplanation / Answer
A) What are the static and dynamic types for the variables
og Static type is Object class Dynamic Type is Game Class
gc Static type is Game class Dynamic Type is Chess Class
gt Static type is Game class Dynamic Type is ThreeDChess Class
_________________
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?
Ans) Game gx = gc; this statement will compile
gc is of type Game class which can pointed to the sub class Chess Class Obejct
now we are assigning to the valriable gx which is of type Game class.
So we wont get and compilation error because Super class reference variable can refer to the sub class object.
Game gx = og;
Here og is of type object and which is pointed to the Game class object.But this statement wont compile because we are assigning super class reference variable to the sub class variable gx.Which is wrong.to avoid we can type cast .
Game gx = (Game)og;
_________________
Chess cx = (Chess)gt;
Here we gt is of type Game class which is pointing to the Sub class object(ThreeDChess class).Here we are type casting the gt of type Game class to the Chess class which is valid.Means super class reference variable can refer to the sub class(ThreeDChess) object.
___________________
ThreeDChess tx = (ThreeDChess)gc;
Here gc is of Game class variable which is pointing to the chess class Object.
Now we are type casting the Game class(Super class) type variable to the ThreeDChess(Sub class ) type variable.So Finally ThreeDChess class is the Sub class of Chess class.So Sub class variable cannot point to the Super class variable.Which is wrong.So we will get compilation error.
__________________
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();
gc.players();
this statement will not gives any compilation error.It will not throw any ClassCastException.the output generated by the statement is 2
_________________
og.players();
here og is of type Object class which is pointed to the Game class.So we cannot call the players() method.Which will gives compilation error.To resolve this we have to type the og variable to Game class
(Game(og)).players();
_________________________
t.players();
This will execute with out any compilation error.Byt as the playes() method is not available In the ThreeDChess class object..then it will check whether the players() method is available in the Game class.So players() method in the game class will be executed and generate the output 2.
__________________
((ThreeDChess)gc).hasMoveableBoard();
When this statement is execute we wont get any compilation error but we will get class Cast Exception because,here gc is of Type Game class which is pointing to the Chess class Object.here we are type casting Game(Super class) to the ThreeDChess (Sub class).So ThreeDChess is the sub class to the Chess class.So Sub class variable cannot point to the Super class Object.So we will get Class Clast exception
__________________
((ThreeDChess)gt).hasMoveableBoard();
This statement will execute perfectly without any compilation errors.Here We are type casting Game class(Super) variable to the ThreeDChess(Sub class) variable and this variable is pointing to the ThreeDChess class object.This statement works perfectly.We wont get any compilation error and ClassCastException error.And the output generated is true.
______________Thank You