P
P
Pavel Dudenkov2015-12-25 15:59:23
Java
Pavel Dudenkov, 2015-12-25 15:59:23

Encapsulation, lack of it - a problem?

Hello dear comrades.
I recently attended an interesting Java interview.
There was a question, what is encapsulation - well, in general, he gave everything as it is. And then the interviewer gives me an example: imagine that we are a developer of a large framework, what will be our main problem if there is no encapsulation?
All my answers about non-secure access to service methods and variables, about the fact that the user will see a bunch of guts outside that he doesn't need, and about the fact that the user can break the entire logic of the framework - were skipped with comments "this is not it" :) .
To my counter questions - only crazy eyes and a sly grin :) . The only thing he answered, prompted so that did not help much, - "the creator of the JDK encountered this problem."
So what is our main problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-12-25
@viperz

Lack of encapsulation in this context obviously means an overly extensive public interface in the first place. And the framework developer cannot change the public interface just like that - backward compatibility must be observed. Accordingly, the more ears we have sticking out, the more difficult it is to maintain and develop the framework. It is enough to look at the Bitrix code, for example, to see what this leads to.
I think the question was about this.

B
bromzh, 2015-12-25
@bromzh

Yes, no big problem. It's just that the responsibility for the correct use of the api is shifted from the developer of the api to the developer using that api.
And by the way: class inheritance breaks encapsulation (for protected fields and methods). And private fields/methods can be easily obtained through reflection.
In general, encapsulation in the form of access modifiers is so-so. It is better to implement this through closures and contexts. But in java there is no such thing (and never will be). So that will do.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question