Encapsulation Principles

3

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'll leave everything in English on purpose, 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();
}

Repsondi's c option, although I found the d option quite interesting.

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

Why?

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

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

Eclipse, for example, does not generate getter s in this way ( ggas ).

Not even lombok does this implementation.

Explanation of Kaplan's response:

  

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   . 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.

    
asked by anonymous 27.02.2017 / 17:40

1 answer

2

Based on the comments made in the question and in some research, I take the following response:

  

Getting getters with clone() is in fact the best / correct implementation. All in all, you have to analyze case by case as you can create several objects and even overload the Garbage Collector .

     Returning the direct reference of the attribute, according to the literature, is not a good encapsulation practice because returning the direct reference allows you to make modifications to the object through the variable assigned with the getters getter return.

    
06.03.2017 / 00:13