OCA Java SE 8 Class Design - OCA Mock Question Class Design 3








Question

What is the output of the following code?

     1: abstract class Shape { 
     2:   public final void print() { System.out.println("Shape prints");  } 
     3:     public static void main(String[] args) { 
     4:     Shape shape = new Rectangle(); 
     5:     shape.print(); 
     6:   } 
     7: } 
     8: public class Rectangle extends Shape { 
     9:   public void print() { System.out.println("Rectangle prints"); } 
    10: } 
  1. Shape prints
  2. Rectangle prints
  3. The code will not compile because of line 4.
  4. The code will not compile because of line 5.
  5. The code will not compile because of line 9.




Answer



E.

Note

The code doesn't compile, so A and B are incorrect.

line 9 print() is marked as final in the superclass Shape, which means it cannot be overridden.