Java OCA OCP Practice Question 1325

Question

Which statement about the following application is true?

package party; //from w  w  w.j a v a  2s  . c  om
? 
abstract class Shape { 
   protected abstract Object getArea(); 
} 
abstract class Rectangle extends Shape { 
   abstract Object getArea(Object list); 
} 
abstract public class Main extends Shape { 
   protected abstract Object getArea(); 
   public static void main(String[] squareFootage) { 
      System.out.print("Rectangle!"); 
   } 
} 
  • A. It compiles and at runtime prints Rectangle!
  • B. It does not compile for one reason.
  • C. It does not compile for two reasons.
  • D. It does not compile for three reasons.


A.

Note

It looks like getArea() in the Rectangle class is an invalid override of the version in the Shape class since package-private is a more restrictive access modifier than protected, but the parameter list changes; therefore, this is an overloaded method, not an overridden one.

The Main class is abstract so no object is instantiated, but there is no requirement that an abstract class cannot contain a runnable main() method.

For these reasons, the code compiles and runs without issue, making Option A correct.




PreviousNext

Related