Java SE 8 Programmer II — Question 163
Given:
1. abstract class Shape {
2. Shape ( ) { System.out.println ("Shape"); }
3. protected void area ( ) { System.out.println ("Shape"); }
4. }
5.
6. class Square extends Shape {
7. int side;
8. Square int side {
9. /* insert code here */
10. this.side = side;
11. }
12. public void area ( ) { System.out.println ("Square"); }
13. }
14. class Rectangle extends Square {
15. int len, br;
16. Rectangle (int x, int y) {
17. /* insert code here */
18. len = x, br = y;
19. }
20. void area ( ) { System.out.println ("Rectangle"); }
21. }
Which two modifications enable the code to compile? (Choose two.)
Answer options
- 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.side = x;
- F. At line 20, use public void area ( ) {
Correct answer: D, F
Explanation
Option D is correct because it properly calls the constructor of the superclass Square, which is necessary for initialization. Option F is also correct as it ensures that the area method is accessible outside the class, aligning with standard Java access modifiers. The other options either do not address constructor calls appropriately or alter access modifiers in a way that is not needed.