nerdexam
Oracle

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.

Working with Methods and Encapsulation

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

1Z0-803 question #197 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)
  • A
    7% (2)
  • B
    80% (24)
  • C
    13% (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.

ARemove the area field.

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.

BChange the getArea( ) method as follows:Correct

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.

CAdd the following method:

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.

DChange the cacess modifier of the SerRadius ( ) method to be protected.Correct

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

#encapsulation#refactoring#access modifiers#methods

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice