Java OCA OCP Practice Question 128

Question

Given:

class Action { 
  private final void doAction() {
      System.out.println("Action"); 
  } /*from w  w w  .  j av a 2s  . c  om*/
} 

public class Button extends Action { 
  public final void doAction() {
     System.out.println("Button");  
  } 
  public static void main(String [] args) { 
    new Button().doAction(); 
  } 
} 

What is the result?

A.   Button 
B.   Action 
C.   Action 
     Button 
D.   Button 
     Action 
E.   Compilation fails 


A is correct.

Note

Although a final method cannot be overridden, in this case, the method is private, and therefore hidden.

The effect is that a new, accessible, method doAction() is created.

Therefore, no polymorphism occurs in this example, the method invoked is simply that of the child class, and no error occurs.

B, C, D, and E are incorrect based on the preceding.




PreviousNext

Related