Java OCA OCP Practice Question 464

Question

What will be the result of compiling and running the following code?

class Base{ // w  w  w .j a  v  a2  s .  c om
   public short getValue (){ return 1;  } //1 
} 
class Base2 extends Base{ 
   public byte getValue (){ return 2;  } //2 
} 
public class Main{ 
   public static void main (String [] args){ 
      Base b = new Base2 (); 
      System.out.println (b .getValue ()); //3 
    } 
} 

Select 1 option

  • A. It will print 1
  • B. It will print 2.
  • C. Compile time error at //1
  • D. Compile time error at //2
  • E. Compile time error at //3


Correct Option is  : D

Note

In case of overriding, the return type of the overriding method must match exactly to the return type of the overridden method if the return type is a primitive.

In case of objects, the return type of the overriding method may be a subclass of the return type of the overridden method.




PreviousNext

Related