Java OCA OCP Practice Question 769

Question

Which of the following lines of code that,

when inserted at line 1,

will make the overriding method in SubClass invoke the overridden method in MyBaseClass on the current object with the same parameter.

class MyBaseClass {
   public void print(String s) {
      System.out.println("MyBaseClass  :" + s);
   }/*ww w  .jav a  2s.  c  o  m*/
}

class Main extends MyBaseClass {
   public void print(String s) {
      System.out.println("Main  :" + s);
      // Line 1
   }

   public static void main(String args[]) {
      Main sc = new Main();
      sc.print("location");
   }
}

Select 1 option

  • A. this.print (s);
  • B. super.print (s);
  • C. print (s);
  • D. MyBaseClass.print (s);


Correct Option is  : B

Note

For Option B.

This is the right syntax to call the base class's overridden method.

There is no way call a method if it has been overridden more than once.

For Option D. print is not a static method.




PreviousNext

Related