lovespring
lovespring

Reputation: 19579

In OOP programming style, why should we hide object's data member from being directly accessed by others

I just don't know why this is the RULE. and what benifit of this rule?
Could you give me a example that we better follow this rule.

Upvotes: 3

Views: 1119

Answers (4)

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26709

It's also related to the separation of concerns. If you have public interface and the data is not public, you can change the way the data is represented any time, changing only the class that holds the data. If the data is not hidden and you change it, you have to change all of the code that uses the data.

Upvotes: 0

Jared Kells
Jared Kells

Reputation: 7062

"Preventing users of your class misusing it" is often touted as the reason that encapsulation is so important.

I think that has an implication that you are writing classes for other un-trusted developers to use, which I think is rarely the case. The un-trusted clients argument confuses the issue.

Most of the time the users of your class are "you" and members of your team.

The public methods and properties of your class make up the interface point between your class and the rest of your code. The smaller that interface is the easier it is to use and understand.

The reason you encapsulate is to make the interface for your class as small and succinct as possible.

If your classes are highly cohesive and have small interfaces you can easily "forget" about how they work and focus on another part of your program.

Take the example of a class that makes web requests. It may expose a single public method DownloadFile(url). This class could be extremely complicated but it's simple interface means you can forget about the internals of how it works leaving you more room in your head to focus on the problem you are trying to solve.

The counter example would be a web request class that exposed all it's methods publicly. It make have 20 methods, DownloadBegin, DownloadEnd, ChooseProtocol, etc etc. All of those may be used internally but were never intended to be called externally. In order to use the class you then have to know how it works internally before you can know which methods to call.

Upvotes: 3

Michael Slade
Michael Slade

Reputation: 13877

One of the virtues of data hiding that gets touted a lot is that it helps to protect your class from misuse. You can't trust the users of your class to do the right thing with it, so you make it impossible to do the wrong thing with it. Most of the time giving a user of your class direct access to any of its members opens up the possibility for that member to be set to some invalid or nonsensical value, or set at the wrong time.

One of the more practical reasons is, you can't change the implementation of a data member. If you have, say, a size member that you make publicly accessible, then later you need to have the class actually do something in response to a change of size, you're stuck. If you have accessor methods, then these methods can be as magical as they need to be.

Upvotes: 1

duncanportelli
duncanportelli

Reputation: 3229

It is also called data hiding which helps to maintain the integrity of the object. It saves the data from misuse and outside interference. The data cannot be accessed directly but access controls can be specified in order to obtain the information. The data or object can be made public or private depending on the needs. The data which is private is not accessible outside the scope of the object. When the data is public it can be accessed by the other parts of the program.

Upvotes: 4

Related Questions