U
U
user_of_toster2021-03-22 11:29:19
OOP
user_of_toster, 2021-03-22 11:29:19

Is this a violation of encapsulation?

Pseudocode:

class Changeable {
    private state: string;
    public createSnapShot(): Snapshot {
         new Snapshot(state);
    }
}

class Snapshot {
    private state: string
    public Snapshot(string state1) {
         state = state1; 
};
    public getState() {
        print(state);
    }
}

Can we say that a method getState()breaks encapsulation Changeableby exposing a private Changeable field to a public method? If not, in what case would the encapsulation be broken?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2021-03-22
@user_of_toster

Is a violation.
More precisely, there is such a situation - you seem to have hidden it behind a method, but it seems like you have revealed it :) That is, you simulated enapsulation to violate it
. Getters / setters and the problem with encapsulation in Symfo ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question