Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. og
  2. gc
  3. 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?

  1. Game gx = gc;
  2. Game gx = og;
  3. Game gx = (Game)og;
  4. Chess cx = (Chess)gt;
  5. 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

  1. gc.players();
  2. og.players();
  3. t.players();
  4. ((ThreeDChess)gc).hasMoveableBoard();
  5. ((ThreeDChess)gt).hasMoveableBoard();

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

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions