1Z0-803 · Question #33
Given: What is the reference type of myZ and what is the type of the object it references?
The correct answer is C. Reference type is Z; object type is Y.. This question assesses the understanding of polymorphism in object-oriented programming by distinguishing between the declared type of a reference variable (reference type) and the actual type of the object it points to (object type).
Question
Given:
What is the reference type of myZ and what is the type of the object it references?
Exhibit
Options
- AReference type is Z; object type is Z.
- BReference type is Y; object type is Y.
- CReference type is Z; object type is Y.
- DReference type is X; object type is Z.
How the community answered
(25 responses)- A12% (3)
- B4% (1)
- C80% (20)
- D4% (1)
Why each option
This question assesses the understanding of polymorphism in object-oriented programming by distinguishing between the declared type of a reference variable (reference type) and the actual type of the object it points to (object type).
This option would be correct if `myZ` referred to an object of type `Z`, for example, if the code was `Z myZ = new Z();`.
This option would be correct if the variable was declared as type `Y` and referenced an object of type `Y`, such as `Y myY = new Y();`.
The reference type is the declared type of the variable `myZ`, which is `Z`. The object type is the actual class of the instance being referenced, which is `Y`. This is a fundamental concept of polymorphism, where a supertype reference (`Z`) can point to a subtype object (`Y`), assuming `Y` extends or implements `Z`.
This option is incorrect because it misidentifies both the reference type as `X` and the object type as `Z`, which does not align with the most common polymorphic scenarios that yield the correct answer C.
Concept tested: Object-oriented polymorphism and type identification
Source: https://docs.oracle.com/javase/tutorial/java/concepts/polymorphism.html
Topics
Community Discussion
No community discussion yet for this question.
