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








Question

Which statement(s) are correct about the following code? (Choose all that apply)

     public class Shape { 
       protected static Integer print() throws Exception { 
         System.out.println("Shape is printing"); 
         return 1; 
       } 
     } 
     public class Rectangle extends Shape { 
       public Number print() throws RuntimeException { 
         System.out.println("Rectangle is printing"); 
         return 2; 
       } 
     } 
  1. It will compile without issue.
  2. It does not compile because the type of the exception the method throws is a subclass of the type of exception the parent method throws.
  3. It does not compile because the return types are not covariant.
  4. It does not compile because the method is protected in the parent class and public in the subclass.
  5. It does not compile because of a static modifier mismatch between the two methods.




Answer



C, E.

Note

The code doesn't compile, so A is incorrect.

B is not correct since overriding a method allows a subclass to define a method with an exception that is a subclass of the exception in the parent method.

C is correct because the return types are not covariant; Number is not a subclass of Integer.

D is incorrect because the subclass defines a method that is more accessible than the method in the parent class, which is allowed.

E is correct because the method is declared as static in the parent class and not so in the child class.