Java OCA OCP Practice Question 806

Question

Consider the following classes...

class Shape{ 
   public int gearRatio = 8; 
   public String accelerate ()  {  return "Item  : Shape";   } 
} 
class Main extends Shape{ 
   public int gearRatio = 9; 
   public String accelerate ()  {  return  "Item  : Main";   } 
   public static void main (String [] args){ 
      Shape c = new Main (); 
      System .out.println ( c.gearRatio+"  "+c.accelerate () ); 
    } /*ww w . ja  v a2 s . c o  m*/
} 

What will be printed when Main is run?

Select 1 option

  • A. 8 Item : Shape
  • B. 9 Item : Shape
  • C. 8 Item : Main
  • D. 9 Item : Main
  • E. None of the above.


Correct Option is  : C

Note

The variables are shadowed and methods are overridden.

Method to be executed depends on the class of the actual object the variable is referencing to.

c refers to object of class Main so Main's accelerate() is selected.




PreviousNext

Related