Questions tagged [encapsulation]

Encapsulation is the principle of bundling software elements in a way to hide implementation details and to expose only a known interface.

Encapsulation is the principle of bundling software elements in a way to hide implementation details and to expose only a known interface.

The concept was first exposed by D. Parnas in the context of modular software development. It was at the origin of the module concept (e.g. adopted in modula-2, or as packages in ADA83), and became the corner stone of OOP classes.

See also:

205 questions
276
votes
17 answers

Why have private fields, isn't protected enough?

Is the visibility private of class fields/properties/attributes useful? In OOP, sooner or later, you are going to make a subclass of a class and in that case, it is good to understand and be able to modify the implementation completely. One of the…
Adam Libuša
  • 2,067
  • 2
  • 10
  • 14
239
votes
15 answers

Why do we need private variables?

Why do we need private variables in classes? Every book on programming I've read says this is a private variable, this is how you define it but stops there. The wording of these explanations always seemed to me like we really have a crisis of trust…
mwallace
  • 2,444
  • 3
  • 14
  • 10
93
votes
15 answers

TDD Red-Green-Refactor and if/how to test methods that become private

as far as I understand it, most people seem to agree that private methods should not be tested directly, but rather through whatever public methods call them. I can see their point, but I have some problems with this when I try to follow the "Three…
Henrik Berg
  • 979
  • 7
  • 6
89
votes
11 answers

Using third-party libraries - always use a wrapper?

Most projects I am involved with use several open-source components. As a general principle, is it a good idea always to avoid binding all components of the code to the third-party libraries and instead go via an encapsulating wrapper to avoid the…
lotsoffreetime
  • 1,121
  • 1
  • 8
  • 8
66
votes
13 answers

Why is it a good idea for "lower" application layers not to be aware of "higher" ones?

In a typical (well-designed) MVC web app, the database is not aware of the model code, the model code is not aware of the controller code, and the controller code is not aware of the view code. (I imagine you could even start as far down as the…
Jason Swett
  • 1,860
  • 1
  • 16
  • 19
64
votes
10 answers

Do you generally send objects or their member variables into functions?

Which is generally accepted practice between these two cases: function insertIntoDatabase(Account account, Otherthing thing) { database.insertMethod(account.getId(), thing.getId(), thing.getSomeValue()); } or function insertIntoDatabase(long…
45
votes
8 answers

What should be allowed inside getters and setters?

I got into an interesting internet argument about getter and setter methods and encapsulation. Someone said that all they should do is an assignment (setters) or a variable access (getters) to keep them "pure" and ensure encapsulation. Am I right…
Botond Balázs
  • 1,463
  • 2
  • 12
  • 13
40
votes
6 answers

Should I place functions that are only used in one other function, within that function?

Specifically, I'm writing in JavaScript. Let's say my primary function is Function A. If Function A makes several calls to Function B, but Function B is not used anywhere else, then should I just place Function B within Function A? Is that good…
36
votes
7 answers

Do you need to think about encapsulation if you can ensure immutability?

Encapsulation 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.1 Encapsulation is used to hide…
33
votes
5 answers

Why do we need enums in dynamically typed languages?

I was reading some code here and saw that an enum is used to store names of html tags. Why do we ever need to do this? What benefit do I get using this strategy? I know that how useful enums are in compiled or statically typed languages but when I…
CodeYogi
  • 2,156
  • 1
  • 18
  • 34
27
votes
10 answers

Is it a code smell if a private method calls a public one?

Is it a code smell to call public method in private method of the same object instance?
Eimantas
  • 1,026
  • 1
  • 8
  • 17
27
votes
7 answers

Why did Java make package access default?

I'm asking this question because I believe they did it for a very good reason and that most people do not use it properly, well from my experience in industry so far anyway. But if my theory is true then I'm not sure why they included the private…
26
votes
4 answers

What does it mean when one says “Encapsulate what varies”?

One of the OOP principles I came across is: -Encapsulate what varies. I understand what the literal meaning of the phrase is i.e. hide what varies. However, I don't know how exactly would it contribute to a better design. Can someone explain it…
26
votes
4 answers

Why Java doesn't make use of encapsulation with some classes?

My question is related with System.in and System.out classes (there might be others like those in the Standard library). Why is that? Isn't that a bad practice in OOP? Shouldn't it be used like: System.getIn() and System.getOut()? I've always had…
Clawdidr
  • 363
  • 2
  • 8
24
votes
7 answers

If a variable has getter and setter, should it be public?

I have a class with a variable that is private and the class has a getter and a setter for that variable. Why not make that variable public? The only case I think you have to use getters and setters is if you need to do some operation besides the…
Oni
  • 927
  • 1
  • 9
  • 13
1
2 3
13 14