Questions tagged [encapsulation]

In OOP, mechanism for restricting access to some of the object's components or a design principle encouraging decoupling from implementation details.

In a programming language encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  • A language mechanism for restricting access to some of the object's components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of object oriented programming, while other programming languages which provide lexical closures view encapsulation as a feature of the language orthogonal to object orientation.

The second definition is motivated by the fact that in many OOP languages hiding of components is not automatic or can be overridden; thus, information hiding is defined as a separate notion by those who prefer the second definition.

(quote from Wikipedia)

2032 questions
81
votes
15 answers

How abstraction and encapsulation differ?

I am preparing for an interview and decided to brush up my OOP concepts. There are hundreds of articles available, but it seems each describes them differently. Some says Abstraction is "the process of identifying common patterns that have …
Aparan
  • 1,263
  • 3
  • 12
  • 17
79
votes
27 answers

Can I access private members from outside the class without using friends?

Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot. I'm mainly looking to see if can be done. Now that that's out of the…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
76
votes
9 answers

"public" or "private" attribute in Python ? What is the best way?

In Python, I have the following example class : class Foo: self._attr = 0 @property def attr(self): return self._attr @attr.setter def attr(self, value): self._attr = value @attr.deleter def…
Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
72
votes
2 answers

Why doesn't PHP permit private const?

I have a class that benefits from the use of constants in its internal implementation, but I would like to limit visibility of these constants. Why doesn't PHP permit private constants? Is there another way to achieve this or is PHP trying to…
leo
  • 2,022
  • 2
  • 17
  • 18
72
votes
7 answers

What is wrong with making a unit test a friend of the class it is testing?

In C++, I have often made a unit test class a friend of the class I am testing. I do this because I sometimes feel the need to write a unit test for a private method, or maybe I want access to some private member so I can more easily setup the state…
cchampion
  • 7,607
  • 11
  • 41
  • 51
69
votes
3 answers

How to hide public methods from IntelliSense

I want to hide public methods from the IntelliSense member list. I have created an attribute that, when applied to a method, will cause the method to be called when its object is constructed. I've done this to better support partial classes. The…
Jordan
  • 9,642
  • 10
  • 71
  • 141
67
votes
7 answers

Can There Be Private Extension Methods?

Let's say I have a need for a simple private helper method, and intuitively in the code it would make sense as an extension method. Is there any way to encapsulate that helper to the only class that actually needs to use it? For example, I try…
David
  • 208,112
  • 36
  • 198
  • 279
61
votes
20 answers

Encapsulation vs Data Hiding - Java

Interviewer: What is encapsulation and how do you achieve it in Java? Me: Encapsulation is a mechanism to hide information from the client. The information may be data or implementation or algorithm. We achieve this using access modifiers.…
Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121
57
votes
10 answers

Why "private" methods in the object oriented?

I understand it is a very basic concept in the oops. But still I cannot get my head around. I understood why member variables are private, so class user cannot abuse it by setting up invalid values. But how can this apply to the methods ?
pointlesspolitics
  • 2,430
  • 6
  • 25
  • 23
57
votes
25 answers

What's the difference between abstraction and encapsulation?

In interviews I have been asked to explain the difference between abstraction and encapsulation. My answer has been along the lines of Abstraction allows us to represent complex real world in simplest manner. It is the process of identifying the…
Vishvadeep singh
  • 1,624
  • 1
  • 19
  • 31
56
votes
5 answers

How do I return a reference to something inside a RefCell without breaking encapsulation?

I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior…
Drew
  • 8,675
  • 6
  • 43
  • 41
54
votes
8 answers

method without access modifier

Ok this is bugging me.. I know I've read it somewhere and google isn't helping. What is the accessibility level of a method that does not specify an access modifier? void Foo() { //code } I want to say internal but I'm not 100% sure.
Leroy Jenkins
  • 2,680
  • 6
  • 25
  • 33
52
votes
7 answers

Why does std::pair expose member variables?

From http://www.cplusplus.com/reference/utility/pair/, we know that std::pair has two member variables, first and second. Why did the STL designers decide to expose two member variables, first and second, instead of offering a getFirst() and a…
guo
  • 9,674
  • 9
  • 41
  • 79
49
votes
16 answers

Set and Get Methods in java?

How can I use the set and get methods, and why should I use them? Are they really helpful? And also can you give me examples of set and get methods?
user759630
  • 679
  • 2
  • 8
  • 11
48
votes
5 answers

How to declare a 'protected' variable in swift

I want to create a class that inherits from another class, which is in a different file. For example: Class1.swift class Class1 { protected var //Do Stuff } Class2.swift class Class2:Class1 { //Do stuff } How would I be able to have…
iProgram
  • 6,057
  • 9
  • 39
  • 80