OCP Question 41, Explanation

Given:

1. abstract class Comsat {
2. Comsat( ) { System.out.println ("Communication Satellite"); }
3. protected void transmit( ) { System.out.println ("Communication Satellite"); }
4. }
5.
6. class Constellation extends Comsat {
7. int num;
8. Constellation(int num) {
9.  /* insert code here */
10. this.num = num;
11. }
12.  public void transmit( ) { System.out.println ("Constellation of comsats"); }
13. }
14. class Galileo extends Constellation {
15. int height, incl;
16. Galileo(int x, int y) {
17. /* insert code here */
18. height = x; incl = y;
19. }
20. void transmit( ) { System.out.println ("Galileo"); }
21.}

Which two modifications enable the code to compile?

A. At line 1, remove abstract
B. At line 9, insert super ( );
C. At line 12, remove public
D. At line 17, insert super(x);
E. At line 17, insert super(); super.num = x;
F. At line 20, use public void transmit( ) {

 

The correct answer is DF.

 

It’s Galileo who fails compilation because the class is unable to invoke its super’s zero-arg ctor, which is simply non-existent. In addition, Galileo’s transmit() has a weaker access privilege than the overridden method in Constellation.

Accordingly, our task is first to make sure Galileo calls the only ctor in Constellation and then elevate transmit()’s access rights.

Since only options D through F have anything to do with Galileo, we immediately cross out options A through C.

Option E cannot work as Constellation doesn’t provide a zero-arg constructor and, what’s more, can’t have a default ctor inserted by the compiler.

Leave Comment

Your email address will not be published.