Java OCA OCP Practice Question 1278

Question

Which statement about the following class is correct?

package shapes;? 
abstract class Shape { 
   abstract String getDescription(); 
} 

class RightShape extends Shape { 
   protected String getDescription() { return "rt"; } // g1 
} 
public abstract class Main extends RightShape { // g2 
   public String getDescription() { return "irt"; } 
   public static void main(String[] edges) { 
      final Shape shape = new Main(); // g3 
      System.out.print(shape.getDescription()); 
   } //from  ww  w .j  a  v a  2  s  . co m
} 
  • A. The code does not compile due to line g1.
  • B. The code does not compile due to line g2.
  • C. The code does not compile due to line g3.
  • D. The code compiles and runs without issue.


C.

Note

The code does not compile, so Option D is incorrect.

The Main class is abstract; therefore, it cannot be instantiated on line g3.

Only concrete classes can be instantiated, so the code does not compile.

Option C is the correct answer.

The rest of the lines of code compile without issue.

A concrete class can extend an abstract class, and an abstract class can extend a concrete class.

The override of getDescription() has a widening access modifier, which is fine per the rules of overriding methods.




PreviousNext

Related