Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private vs. Public members in practice (how important is encapsulation?) [closed]

Tags:

One of the biggest advantages of object-oriented programming is encapsulation, and one of the "truths" we've (or, at least, I've) been taught is that members should always be made private and made available via accessor and mutator methods, thus ensuring the ability to verify and validate the changes.

I'm curious, though, how important this really is in practice. In particular, if you've got a more complicated member (such as a collection), it can be very tempting to just make it public rather than make a bunch of methods to get the collection's keys, add/remove items from the collection, etc.

Do you follow the rule in general? Does your answer change depending on whether it's code written for yourself vs. to be used by others? Are there more subtle reasons I'm missing for this obfuscation?

like image 835
Asmor Avatar asked Sep 19 '08 04:09

Asmor


People also ask

Is encapsulation private or public?

Encapsulation is when we define the access level of our classes. The public access modifier doesn't impose any restrictions. The private access modifier only allows members to be accessed within the current class, not outside of it (through an object).

What is the importance of encapsulation?

Advantages of Encapsulation Encapsulation protects an object from unwanted access by clients. Encapsulation allows access to a level without revealing the complex details below that level. It reduces human errors.

Can encapsulation be public?

Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. It is more defined with the setter and getter method.

How can encapsulation be achieved using only private members?

How can Encapsulation be achieved? Explanation: Using access specifiers we can achieve encapsulation. Using this we can in turn implement data abstraction. It's not necessary that we only use private access.


1 Answers

It depends. This is one of those issues that must be decided pragmatically.

Suppose I had a class for representing a point. I could have getters and setters for the X and Y coordinates, or I could just make them both public and allow free read/write access to the data. In my opinion, this is OK because the class is acting like a glorified struct - a data collection with maybe some useful functions attached.

However, there are plenty of circumstances where you do not want to provide full access to your internal data and rely on the methods provided by the class to interact with the object. An example would be an HTTP request and response. In this case it's a bad idea to allow anybody to send anything over the wire - it must be processed and formatted by the class methods. In this case, the class is conceived of as an actual object and not a simple data store.

It really comes down to whether or not verbs (methods) drive the structure or if the data does.

like image 176
Kyle Cronin Avatar answered Sep 22 '22 21:09

Kyle Cronin