1Z0-803 · Question #170
The protected modifier on a Field declaration within a public class means that the field ______________.
The correct answer is D. Can be read and written from this class and its subclasses defined in any package. The protected access modifier allows a field to be accessed and modified by its own class, other classes within the same package, and by all subclasses regardless of their package location.
Question
The protected modifier on a Field declaration within a public class means that the field ______________.
Options
- ACannot be modified
- BCan be read but not written from outside the class
- CCan be read and written from this class and its subclasses only within the same package
- DCan be read and written from this class and its subclasses defined in any package
How the community answered
(34 responses)- A6% (2)
- B3% (1)
- D91% (31)
Why each option
The `protected` access modifier allows a field to be accessed and modified by its own class, other classes within the same package, and by all subclasses regardless of their package location.
The `protected` modifier controls visibility and access, not immutability; a field's ability to be modified is determined by whether it is `final`.
`protected` access allows both read and write operations, and its scope extends to subclasses in any package, not just read-only access from outside the immediate class.
While `protected` grants access to subclasses, it applies to subclasses in *any* package, not exclusively to those within the same package.
The `protected` access modifier permits members to be accessed by any class within the same package as the protected member's class, and by subclasses of the protected member's class, even if those subclasses are located in different packages. This scope grants both read and write capabilities for the field under these specified conditions.
Concept tested: Java protected access modifier scope
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/accessmodifiers.html
Topics
Community Discussion
No community discussion yet for this question.