Question

Consider the classes Sphere and Ball described in class and in Chapter 9 (beginning on page...

Consider the classes Sphere and Ball described in class and in Chapter 9 (beginning on page 457). Consider a variation on the classes where Ball overrides both the area() and displayStatistics() methods, and the displayStatistics() methods in each one calls area(). You have b1 and s1 as declared below:

Sphere s1 = new Sphere();

Ball b1 = new Ball();

For each scenario below, indicate if the assignments are legal or illegal. In addition for legal assignments also explain which version of area each all to displayStatistics() invokes.

  1. s1.displayStatistics();

b1.displayStatistics();

  1. s1.displayStatistics();

s1 = b1;

s1.displayStatistics();

b1.displayStatistics();

  1. s1.displayStatistics();

b1 = s1;

s1.displayStatistics();

b1.displayStatistics();

Homework Answers

Answer #1

In the above question, as Ball overrides both the area() and display statisticts() so the Ball class is the subclass and the Sphere class is the parent class.

1) s1.displayStatistics();

b1.displayStatistics();

This is an legal assignment.

The area() method that the above assignment will invoke.

s1.displayStatistics(s1.area());

b1.displayStatistics(b1.area());

2) s1.displayStatistics();

s1 = b1;

s1.displayStatistics();

b1.displayStatistics();

This is an legal assignment.

The area() method that the above assignment will invoke.

s1.displayStatistics(s1.area());

s1 = b1;

s1.displayStatistics(b1.area());

b1.displayStatistics(b1.area());

3) s1.displayStatistics();

b1 = s1;

s1.displayStatistics();

b1.displayStatistics();

This is an illegal assignment.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions