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
Correct Option is : C
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.