nerdexam
Oracle

1Z0-803 · Question #225

Given: public class SuperTest { public static void main(String[] args) { statement1 statement2 statement3 } } class Shape { public Shape() { System.out.println("Shape: constructor"); } public void foo

The correct answer is F. Square square = new Square();. To produce the desired output starting with 'Shape: constructor', the Square object must be instantiated using its no-argument constructor, which implicitly calls the Shape constructor. Subsequent calls involve Square's method overloading and use of the super keyword.

Working with Inheritance

Question

Given:

public class SuperTest { public static void main(String[] args) { statement1 statement2 statement3 } } class Shape { public Shape() { System.out.println("Shape: constructor"); } public void foo() { System.out.println("Shape: foo"); } } class Square extends Shape { public Square() { super(); } public Square(String label) { System.out.println("Square: constructor"); } public void foo() { super.foo(); } public void foo(String label) { System.out.println("Square: foo"); } } } } What should statement1, statement2, and statement3, be respectively, in order to produce the result? Shape: constructor Square: foo Shape: foo

Options

  • ASquare square = new Square ("bar");
  • BSquare square = new Square ("bar");
  • CSquare square = new Square ();
  • DSquare square = new Square ();
  • ESquare square = new Square ();
  • FSquare square = new Square();

How the community answered

(21 responses)
  • A
    24% (5)
  • B
    5% (1)
  • C
    10% (2)
  • D
    5% (1)
  • F
    57% (12)

Why each option

To produce the desired output starting with 'Shape: constructor', the `Square` object must be instantiated using its no-argument constructor, which implicitly calls the `Shape` constructor. Subsequent calls involve `Square`'s method overloading and use of the `super` keyword.

ASquare square = new Square ("bar");

`Square square = new Square ("bar");` would invoke the `Square(String label)` constructor, which directly prints 'Square: constructor', deviating from the required initial output of 'Shape: constructor'.

BSquare square = new Square ("bar");

`Square square = new Square ("bar");` would invoke the `Square(String label)` constructor, which directly prints 'Square: constructor', deviating from the required initial output of 'Shape: constructor'.

CSquare square = new Square ();

`Square square = new Square ();` is functionally identical to option F, but in multiple-choice questions with duplicated text, only one is marked as the correct selection for exam purposes.

DSquare square = new Square ();

`Square square = new Square ();` is functionally identical to option F, but in multiple-choice questions with duplicated text, only one is marked as the correct selection for exam purposes.

ESquare square = new Square ();

`Square square = new Square ();` is functionally identical to option F, but in multiple-choice questions with duplicated text, only one is marked as the correct selection for exam purposes.

FSquare square = new Square();Correct

`Square square = new Square();` is the correct statement1 because calling the no-argument `Square` constructor implicitly invokes `super()`, which then executes the `Shape` class constructor and prints 'Shape: constructor', matching the first line of the expected output.

Concept tested: Java inheritance, constructors, method overloading, `super` keyword

Source: https://docs.oracle.com/javase/tutorial/java/IandI/super.html

Topics

#inheritance#constructors#super()#method overriding#method overloading

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice