java – Encapsulation Principles

Question:

I am studying for the Java Programmer SE 7 I certification exam.

I use SelfTest Kaplan (recommended by Oracle itself).

I came across the following question: (I will leave everything in English on purpose. So as not to translate and end up omitting information).

Given:

public class Pedometer {
     private double stride;
     private double[] measurements;
}

Which code fragment is a method that meets good encapsulation
principles?

a) public void getStride(double stride) {
    stride = this.stride;
} 

b) public void getStride(double stride) {
    this.stride = stride;
} 

c) public double[] getMeasurements() {
    return this.measurements;
} 

d) public double[] getMeasurements() {
    return measurements.clone();
}

I answered option c , although I found option d quite interesting.

When I went to see the result I saw that, in fact, the correct option was d .

Why?

I'm not questioning how the clone() method works, and I'm not questioning that cloning an object is "better", but questioning: why is this implementation not common?

I've worked on several Java projects and I've never seen a getter like this.

Eclipse, for example, doesn't generate getters that way ( ggas ).

Not even lombok does this implementation.


Kaplan's explanation of the answer:

Explanation: The following code fragment is a method that meets good
encapsulation principles:

public double[] getMeasurements() {
    return measurements.clone(); 
}

In this code fragment, the accessor method uses the clone method to
return a copy of the measurements field, rather than a reference to
the field itself. Accessor methods should return copies of field
objects. A copy of a field will prevent other classes from modifying
fields directly and will require going through mutator methods.

The two versions of the accessor method getStride do not meet good
encapsulation principles. Neither method returns the value of the
stride field as expected, but instead modifies the stride field like a
mutator method. Also, the statement stride = this.stride; overwrites
the stride parameter, rather than assigning the stride parameter to
the stride field as expected.

The accessor method getMeasurements that returns a direct reference to
the measurements field object does not meet good encapsulation
principles. Accessor methods should not return direct references to
field objects. Direct access will allow other classes to modify fields
directly without going through mutator methods.

Answer:

Based on the comments made in the question and some surveys, I take as an answer that:

Making getters with clone() is indeed the best/correct implementation. However, it is necessary to analyze it on a case-by-case basis, as it can lead to the creation of several objects, and may even overload the Garbage Collector .

Making getters returning the attribute's direct reference, according to the literature, is not a good encapsulation practice because when returning the direct reference you allow modifications to be made to the object through the variable assigned with the return of the getter .

Scroll to Top