Java OCA OCP Practice Question 1439

Question

Consider :

class A  {  public void m (){}   } 
class B extends A  {  public void m (){}   } 
class C extends B  {  public void m (){}   } 

How can you let m () method of A to be called from an instance method in C?

Select 1 option

  • A. ( (A) this ).m ( );
  • B. super.m ( );
  • C. super.super.m ( );
  • D. this.super.m ( );
  • E. It is not possible.


Correct Option is  : E

Note

The method in C needs to call a method in a superclass two levels up.

But super is a keyword and not an attribute so super.

super.m() strategy will not work.

There is no way to go more than one level up for methods.

This problem doesn't occur for instance variables because variable are never overridden.

They are shadowed.

So to access any of the super class's variable, you can unshadow it using a cast.




PreviousNext

Related