Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why encapsulation is an important feature of OOP languages? [closed]

I came across different interview where question was asked to me why encapsulation is used? Whose requirement actually is encapsulation? Is it for users of program? Or is it for co-workers? Or is it to protect code from hackers?

like image 673
Shahzad Avatar asked Aug 18 '13 15:08

Shahzad


People also ask

Why is encapsulation important in object-oriented programming?

Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them.

What are the important features of encapsulation?

In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding. Declare the variables of a class as private. Provide public setter and getter methods to modify and view the variables values.

What is an encapsulation in object-oriented programming?

In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components.


5 Answers

Encapsulation helps in isolating implementation details from the behavior exposed to clients of a class (other classes/functions that are using this class), and gives you more control over coupling in your code. Consider this example, similar to the one in Robert Martin's book Clean Code:

public class Car
{
//...
public float GetFuelPercentage() { /* ... */ };

//...

private float gasoline;
//...
}

Note that the client using the function which gives you the amount of fuel in the car doesn't care what type of fuel does the car use. This abstraction separates the concern (Amount of fuel) from unimportant (in this context) detail: whether it is gas, oil or anything else.

The second thing is that author of the class is free to do anything they want with the internals of the class, for example changing gasoline to oil, and other things, as long as they don't change its behaviour. This is thanks to the fact, that they can be sure that no one depends on these details, because they are private. The fewer dependencies there are in the code the more flexible and easier to maintain it is.

One other thing, correctly noted in the underrated answer by utnapistim: low coupling also helps in testing the code, and maintaining those tests. The less complicated class's interface is, the easier to test it. Without encapsulation, with everything exposed it would be hard to comprehend what to test and how.

To reiterate some discussions in the comments:

  • No, encapsulation is not the most important thing in OOP. I'd dare even to say that it's not very important. Important things are these encouraged by encapsulation - like loose coupling. But it is not essential - a careful developer can maintain loose coupling without encapsulating variables etc. As pointed out by vlastachu, Python is a good example of a language which does not have mechanisms to enforce encapsulation, yet it is still feasible for OOP.

  • No, hiding your fields behind accessors is not encapsulation. If the only thing you've done is write "private" in front of variables and then mindlessly provide get/set pair for each of them, then in fact they are not encapsulated. Someone in a distant place in code can still meddle with internals of your class, and can still depend on them (well, it is of course a bit better that they depend on a method, not on a field).

  • No, encapsulation's primary goal is not to avoid mistakes. Primary goals are at least similar to those listed above, and thinking that encapsulation will defend you from making mistakes is naive. There are just lots of other ways to make a mistake beside altering a private variable. And altering a private variable is not so hard to find and fix. Again - Python is a good example for sake of this argument, as it can have encapsulation without enforcing it.

like image 133
BartoszKP Avatar answered Oct 01 '22 19:10

BartoszKP


Encapsulation prevents people who work on your code from making mistakes, by making sure that they only access things they're supposed to access.

like image 41
SLaks Avatar answered Oct 01 '22 19:10

SLaks


At least in most OO languages, encapsulation is roughly equivalent to the lock on the door of a bathroom.

It's not intended to keep anybody out if they really insist on entering.

It is intended as a courtesy to let people know that entering will lead mostly to:

  1. embarrassment, and
  2. a stinking mess.
like image 43
Jerry Coffin Avatar answered Oct 01 '22 19:10

Jerry Coffin


Encapsulation allows you to formalize your interfaces, separating levels of abstraction (i.e. "application logic accesses IO code only in this and this way").

This in turn, allows you to change the implementation of a module (the data and algorithms inside the module) without changing the interface (and affecting client code).

This ability to modify modules independently of each other, improves your ability to measure your performance and make predictions in a project's deadlines.

It also allows you to test modules separately and reuse them in other projects (because encapsulation also lowers inter-dependencies and improves modularity of your code).

Not enforcing encapsulation tends to lead to a project's failure (the problem grows with the complexity of the project).

like image 32
utnapistim Avatar answered Oct 01 '22 21:10

utnapistim


I architected a fast-track project. Encapsulation reduces the propagation of change through the system. Changes cost time and money.

When code is not encapsulated, a person has to search many files to find where to make the change(s). Adversely, there is the question of "Did I find all the places?" and the other point "what effect to the entire system to do all of these scatter changes have?"

I'm working on an embedded medical device and quality is imperative. Also, all changes must be documented, reviewed, unit tested and finally a system test performed. By using encapsulation, we can reduce the number of changes and their locality, reducing the number of files that must be retested.

like image 26
Thomas Matthews Avatar answered Oct 01 '22 21:10

Thomas Matthews