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.
b1.displayStatistics();
s1 = b1; s1.displayStatistics(); b1.displayStatistics();
b1 = s1; s1.displayStatistics(); b1.displayStatistics(); |
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.
Get Answers For Free
Most questions answered within 1 hours.