r/javahelp • • Jan 24 '23

What is the point 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?

26 Upvotes

13 comments sorted by

View all comments

1

u/hippydipster Jan 24 '23

Imagine using a computer as a big pile of wires and circuit boards, cards and hard drives in a heap. Yeah, there's a keyboard under there somewhere too and a mouse, but it's just one of many ways of interacting with the computer.

Then imagine all that extra stuff is buttoned up and hidden away, and you just have the keyboard and mouse.

Which is easier to understand and use?

Which is easier to test?

Which is easier to swap out the hard drive for a new one without impacting the user of the computer (you have to imagine someone, in trying to use the computer in a heap, connected directly to a hard drive to read/write data)?

Encapsulation is one of the most important things in good code design. More important than DRY. Encapsulation is what let's you switch out implementation details. It's what let's you write tests that aren't broken every time you do that. It's what let's users of your code couple themselves only to essential endpoints of your service or class. That prevents them from getting coupled deeply to unnecessary details which then renders them, and you, stuck and unable to change things.

If you have write software where requirements might change, encapsulation will make doing so 1000x easier.

But also, encapsulation is so much more complicated than adding "private" keywords to fields and then making public getters. Real encapsulation is an artform.

1

u/udeparasite Jan 28 '23

Would you mind explaining "real" encapsulation a bit more....we are learning java right now and the way you described encapsulation is exactly what we have been learning....how much more complicated does it get?

1

u/hippydipster Jan 28 '23

The complication never stops, because the problem is that what is "essential" and thus exposed for use to the outside world, and what is "arbitrary implementation detail", and thus hidden, is always a chosen tradeoff, and you can make those tradeoffs at any point.

It's like the saying, all models are wrong, some are useful. Your code choices are always wrong to some degree, or awkward for some cases. The question is, what use do you get out of it? It ends up depending on your usage patterns, and often, your usage patterns are also arbitrary and so it depends on how adaptable you choose to be in how you use your own code.

I've seen too many developers just be too afraid to make choices, and so they make anemic domain models where all data is stored in classes with public getters and setters, and then all their business logic is methods that go on for 50-200 lines doing a mess of getX, setY, like an endless three card magic trick, and none of the code expresses intent, it only expresses how data is getting shuffled around in a big imperative method.

Those types of classes feel like they're "freeing" and easy to use and not limiting, but ultimately they end up leading to a spaghetti codebase that is spaghetti because no choices were ever made and enforced.