What is the point of encapsulation?
ELI5: Encapsulation in Java
Why do i need to encapsulate my programs?
ELI5: Encapsulation
You can find examples of it even in Android designs, it's just the apps tend to be simpler so class encapsulation is less emphasized. Most of the design principals are less "required" in smaller applications. The principles don't lose their importance per se, you just don't suffer the same degree of consequences if you skip them. The larger a system gets the more important it is that you followed the fundamentals from the beginning.
Encapsulation is a set of implementation specifics over a broader concept.
The main concept is this: don't give outside users of a "thing" (class, method, whatever) access to, control of, or knowledge of the internals of said thing. They should know or have the minimum they need to do their job and find the "thing" useful. (In security the phrase is "principal of least privilege" and while it is different, it follows pretty close logically.)
If I am designing a class, I don't want the outside world (consumer of the class in this context) to know about or have access to my internals, such as:
-
memory -> I can't control my data if I give them access to it
-
adherence to SRP -> (Single Responsibility Principal) - By exposing as little as possible, a small "contract", the object can more easily adhere to SRP. This one is a bit off topic but valuable in class design.
-
implementation details -> I want the abstraction that the class represents to not "leak" its internal implementation details - if I use an array versus some kind of list for storage I don't want the consumer of the class to know about it, instead I expose to them a generic "collection", preferably through an interface. How I store and access the data is my business, how you use what I expose to you is yours.
These design principals are important for a lot of reasons, but to me the most important is the simple fact that things change. The requirements of the system as a whole will change over time and as a result refactoring, extension, and/or modification will be required. The better design a system has, the easier it will be to change and adapt the software to the new conditions. If my abstractions are leaky then when I change the internals of that class I'll also have to go and change all of the consumers of that class. (Not ideal and sometimes that choice will land outside of your control and therefore make it impractical or even impossible to change.)
With respect to security, it follows along the same logic. By not giving them access to my internals I am in control of the changes in state. If I am in control then they can't "hurt" me. Basic GIGO (garbage in, garbage out) -> never trust your user, even when your user may be another developer, even when that other developer may be you.
More on reddit.comWhat is the Difference Between Abstraction and Encapsulation in Java?
Why use encapsulation
What is the principle of encapsulation
Beginner here.
I hear "restrict access" but I don't quite understand why we need to restrict access. What damage could someone do to a java program if it were not encapsulated?
What would happen to say amazon if their payment system was java and not encapsulated?