Java OCA OCP Practice Question 3243

Question

Which statement, when inserted at (1), will call the print() method in the Shape class.

class Shape {
  public void print() {
    System.out.println("base");
  }/*  w w  w . j  av a  2 s . c  o  m*/
}

class Rectangle extends Shape {
  public void print() {
    System.out.println("extension");

    // (1) INSERT CODE HERE.
  }
}

public class Main {
  public static void main(String[] args) {
    Rectangle ext = new Rectangle();
    ext.print();
  }
}

Select the one correct answer.

  • (a) Shape.print();
  • (b) Shape.this.print();
  • (c) print();
  • (d) super.print();
  • (e) this.print();


(d)

Note

Overridden method implementations are accessed using the super keyword.

Statements like print(), Shape.print(), and Shape.this.print() will not work.




PreviousNext

Related