1Z0-803 · Question #197
Given: The class is poorly encapsulated. You need to change the circle class to compute and return the area instead. Which two modifications are necessary to ensure that the class is being properly en
The correct answer is B. Change the getArea( ) method as follows: D. Change the cacess modifier of the SerRadius ( ) method to be protected.. Proper encapsulation for a computed property involves deriving its value on demand and restricting direct external modification of core internal state.
Question
Given:
The class is poorly encapsulated. You need to change the circle class to compute and return the area instead. Which two modifications are necessary to ensure that the class is being properly encapsulated?
Exhibit
Options
- ARemove the area field.
- BChange the getArea( ) method as follows:
- CAdd the following method:
- DChange the cacess modifier of the SerRadius ( ) method to be protected.
How the community answered
(30 responses)- A7% (2)
- B80% (24)
- C13% (4)
Why each option
Proper encapsulation for a computed property involves deriving its value on demand and restricting direct external modification of core internal state.
While removing a redundant area field is good practice when the area is computed, this action alone does not directly ensure encapsulation without the accompanying change in how the area is retrieved (Option B), making it a consequence rather than a primary encapsulating modification.
Changing the getArea() method to compute the area dynamically ensures the area is always consistent with the radius and removes the need to store it as a separate field, improving encapsulation by deriving state.
Adding a new method, without knowing its implementation or access modifier, does not inherently improve encapsulation; it could potentially expose more internal details or provide uncontrolled access points.
Changing the SetRadius() method's access modifier to protected (or private) restricts direct external modification of the radius field, forcing changes through a controlled interface and thereby improving encapsulation of the object's fundamental state.
Concept tested: Encapsulation of Derived State and Mutators
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Topics
Community Discussion
No community discussion yet for this question.
