Abstract Classes and Inheritance in Java: Exam 1Z0-809 Question Solution

Java SE 8 Programmer II Exam: Question Solution

Question

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.)

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E. F.

DF.

The code presents a hierarchy of classes that extends the abstract class Shape, representing geometric shapes. The class Square is a subclass of Shape and represents a square, while the class Rectangle is a subclass of Square and represents a rectangle. The Rectangle class has a constructor that takes two arguments, which represent the length and breadth of the rectangle.

The task is to identify two modifications that would enable the code to compile. Let's examine each option:

A. At line 1, remove abstract This modification would make the Shape class a concrete class, which means it can be instantiated. However, this modification alone would not enable the code to compile because there are still other issues with the code.

B. At line 9, insert super(); This modification would call the constructor of the superclass (i.e., Shape) in the constructor of Square, which is necessary because the Shape constructor has side effects (i.e., printing "Shape" to the console). However, this modification alone would not enable the code to compile because there are still other issues with the code.

C. At line 12, remove public This modification would make the area method in Square have default (package-private) access. However, this modification alone would not enable the code to compile because there are still other issues with the code.

D. At line 17, insert super(x); This modification would call the constructor of the superclass (i.e., Square) in the constructor of Rectangle, passing the value of x as an argument to initialize the side field of Square. This is necessary because Square does not have a no-argument constructor. This modification would enable the code to compile.

E. At line 17, insert super(); super.side = x; This modification is similar to option D, but it explicitly sets the value of the side field in Square after calling the superclass constructor. This modification would enable the code to compile.

F. At line 20, use public void area() This modification would change the access level of the area method in Rectangle to public, which is necessary because it overrides the protected area method in Shape. This modification would enable the code to compile.

Therefore, the correct answers are D and either E or F.