Java SE 8 Programmer II — Question 48
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?
abstract
Answer options
- A. At line 1, remove super ( );
- B. At line 9, insert public
- C. At line 12, remove super (x);
- D. At line 17, insert super (); super.side = x;
- E. At line 17, insert public void area ( ) {
- F. At line 20, use
Correct answer: C, D
Explanation
The correct modifications are C and D. Removing super(x); at line 12 is necessary because the constructor of the parent class is not being called, and it is not required in this context. Additionally, inserting super(); super.side = x; at line 17 ensures that the parent constructor is called while also initializing the side variable, allowing the code to compile successfully.