I have been trying to figure out how/why I should use encapsulation, but for some reason it just isn't clicking. Can anyone clarify? Nothing in the book I am reading at the moment is making it clear.

04-01-2012, 08:04 PM

Wnt2bsleepin

Re: Can someone post an example where encapsulation is useful?

From what I gathered, it is supposed to make it so that a particular class has complete control over what is stored inside it. When you write a class, a person who is using it doesn't need to know how it's implemented. That is, how it actually does what it is supposed to do. Encapsulation prevents them from messing with anything inside the class, be it intentional or accidental.

04-13-2013, 06:56 PM

jim829

Re: Can someone post an example where encapsulation is useful?

This is how I think of it:

Consider a radio. It provides a capability to do something with public controls. However, you don't need access to the resistors, transistors or other discrete components to use the radio. And the original makers can improve the radio by using more efficient and perhaps less expensive components. But it won't affect how you use the radio. But it may cost less and get better reception. The same is true for oo encapsulation. It only exposes those methods and fields necessary to provide a specific function. And later on someone can perhaps improve on those methods without affecting the way it is used by others.

Java as well as other oo languages support extending classes and overriding existing methods which can break encapsulation. So this should be done carefully and in accordance with the extended classes documentation.

Regards,
Jim

04-13-2013, 07:29 PM

kjkrum

Re: Can someone post an example where encapsulation is useful?

It's not something you can easily see an example of, because it's something that becomes more important as the size and complexity of a program grows.

But I can tell you about an example. My job currently involves maintaining and trying to add features to a program that some jackass contractor wrote with basically zero encapsulation. It has tons of global state, and worse, that state is duplicated in several places. It's a huge pain in the ass to figure out when and where a variable was assigned the value that it has, or what effect assigning a different value might have on some distant part of the program. In a properly encapsulated program, all the causes and effects are right there in the class or method you're looking at.

04-13-2013, 07:38 PM

kjkrum

Re: Can someone post an example where encapsulation is useful?

Here's an extremely contrived example. Consider two versions of the same class:

Code:

public class Foo {
public int a;
public int b; // should always be a * 2
}

With the first version, every user of the class has to know that b is supposed to be a * 2. Inevitably something is going to muck it up, and then some other part of the program that relies on b being a * 2 is going to break. With the second version, the class enforces its own rules. Not only do the users of the class not have to worry about the rules, they may not even know what the rules are. This prevents them from depending on the rules, so they won't break if the rules change.

In the real world, the rules that get encapsulated are often things like, "always close socket A and interrupt thread B before closing file C". Expose that kind of rule, and something is inevitably going to screw it up. Encapsulate it, and external code can't screw it up.