I could not quite understand the encapsulation in OOP [duplicate]

4

I did a programming logic course and now I'm doing logic OO, but it came in the encapsulation part and I'm having a little trouble understanding. What would it be and how is it used?

    
asked by anonymous 25.09.2017 / 02:31

1 answer

10

My experience is that most courses are said about object-oriented because everyone wants to learn it. And everyone wants to learn because they say it cures cancer. Which is far from the truth. It's a shame because people keep looking for the sacred chalice of OOP and they do not even learn other forms, they do not learn when to use OO or not, and worse, they usually do not learn the beloved right paradigm.

OOP was not designed to facilitate learning, on the contrary it is much more difficult to learn and do object-oriented code, and only the one who is naive and does not learn right is the one who thinks it is easy, which obviously does everything wrong and does not benefits from what the paradigm promises, but it can not assess, it just trusts that it is having some advantage.

Most OOP codes we see around here are not OOPs or abuse of resources that are not needed.

Do not go to OOP without understanding the imperative very well, but very well indeed. If you still do not understand every detail of the code do not go for more advanced and not-so-needed concepts like that. Almost everyone ignores this tip, it's a shame because those who did not ignore today are good developers and respond here while those who ignore continue to pound head on every code they try to do.

In a nutshell, even because it has multiple definitions and people can not really agree, encapsulation is hiding details about the object and exposing only what establishes itself as API , a guarantee, a contract that will never be changed. Everything that is encapsulated can be changed without problems, as long as it continues to fulfill all that the class proposes. So both state and private behavior is created to meet the requirements of the class without committing itself to a specific way of doing it.

Encapsulation facilitates maintenance because it creates a closed box where the consumer does not need to know the details. Failure to expose these parts will prevent improper or improper use.

It's like a car, you need to know how to drive it, not how it works, including each car can work very differently and you know how to use them all, the differences are encapsulated.

It also helps readability as it does not expose more than necessary. And avoid mistakes by misuse.

You have several questions on the subject, I advise you to read all and search further:

25.09.2017 / 02:53