nerdexam
Oracle

1Z0-803 · Question #158

Given: What is the result?

The correct answer is D. Y Y Y. The program demonstrates method overriding where the subclass's overridden method is consistently invoked due to polymorphism, leading to the repeated output of the subclass's specific value.

Working with Inheritance

Question

Given:

What is the result?

Exhibit

1Z0-803 question #158 exhibit

Options

  • AX X X
  • BX Y X
  • CY Y X
  • DY Y Y

How the community answered

(34 responses)
  • A
    3% (1)
  • B
    6% (2)
  • C
    9% (3)
  • D
    82% (28)

Why each option

The program demonstrates method overriding where the subclass's overridden method is consistently invoked due to polymorphism, leading to the repeated output of the subclass's specific value.

AX X X

This would occur if the superclass's implementation or value ('X') was always accessed, indicating no overriding or incorrect polymorphic dispatch.

BX Y X

This output suggests a mix of superclass and subclass values, which is unlikely for repeated access to a consistently overridden or shadowed member under the same conditions.

CY Y X

This output also suggests a mixed result that doesn't align with a straightforward polymorphic invocation where the subclass's behavior predominates once established.

DY Y YCorrect

This result typically occurs in a scenario where a superclass has a member (e.g., a method or a field accessed via an overridden method) with value 'X', and a subclass overrides that member with value 'Y'. When an instance of the subclass is created and its methods are invoked (possibly three times, or a loop printing three times), the overridden method (or accessor for the shadowed field) from the subclass is called, consistently returning 'Y'.

Concept tested: Polymorphism and method overriding

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

Topics

#polymorphism#method overriding#inheritance

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice