Java OCA OCP Practice Question 1300

Question

Which statement about the following class is correct?

package mypkg; /*  w w  w  .j  ava  2s .co m*/
? 
abstract class Shape { 
   private int getEqualSides() {return 0;} 
} 
abstract class Rectangle extends Shape { 
   public static int getEqualSides() {return 2;} // x1 
} 
public final class Square extends Rectangle { 
   public int getEqualSides() {return 4;} // x2 
   
   public static void main(String[] corners) { 
      final Square myFigure = new Square(); // x3 
      System.out.print(myFigure.getEqualSides()); 
   } 
} 
  • A. The code does not compile due to line x1.
  • B. The code does not compile due to line x2.
  • C. The code does not compile due to line x3.
  • D. The code compiles and runs without issue.


B.

Note

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

The issue here is that the override of getEqualSides() in Square is invalid.

A static method cannot override a non-static method and vice versa.

For this reason, Option B is the correct answer.




PreviousNext

Related