Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

identify the main types of research studies in HRM research;

Answered: 1 week ago

Question

4. Who would lead the group?

Answered: 1 week ago

Question

2. What type of team would you recommend?

Answered: 1 week ago