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.
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)- A24% (5)
- B5% (1)
- C10% (2)
- D5% (1)
- F57% (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.
`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'.
`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'.
`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.
`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.
`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.
`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
Community Discussion
No community discussion yet for this question.