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
1234
votes
17 answers

What is the difference between public, private, and protected inheritance in C++?

What is the difference between public, private, and protected inheritance in C++? All of the questions I've found on SO deal with specific cases.
user106599
828
votes
12 answers

Why are Python's 'private' methods not actually private?

Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: __myPrivateMethod(). How, then, can one explain this >>>> class MyClass: ... def…
willurd
  • 11,745
  • 5
  • 28
  • 23
446
votes
40 answers

Difference between abstraction and encapsulation?

What is the precise difference between encapsulation and abstraction?
Rocky
  • 4,471
  • 3
  • 18
  • 5
394
votes
31 answers

When should you use 'friend' in C++?

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language. What is a good example of using friend? Reading the FAQ a bit longer I like…
roo
  • 7,106
  • 8
  • 39
  • 45
223
votes
5 answers

What is the C# equivalent of friend?

Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like the private member variables of a class to be accessible to a Tester class without exposing them to other classes. In C++ I'd just declare the Tester class as a…
Ben L
  • 6,618
  • 8
  • 39
  • 34
186
votes
9 answers

Should I return a Collection or a Stream?

Suppose I have a method that returns a read-only view into a member list: class Team { private List players = new ArrayList<>(); // ... public List getPlayers() { return Collections.unmodifiableList(players); …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
185
votes
22 answers

Abstraction VS Information Hiding VS Encapsulation

Can you tell me what is the difference between abstraction and information hiding in software development? I am confused. Abstraction hides detail implementation and information hiding abstracts whole details of something. Update: I found a good…
popopome
  • 12,250
  • 15
  • 44
  • 36
146
votes
21 answers

Must Dependency Injection come at the expense of Encapsulation?

If I understand correctly, the typical mechanism for Dependency Injection is to inject either through a class' constructor or through a public property (member) of the class. This exposes the dependency being injected and violates the OOP principle…
urig
  • 16,016
  • 26
  • 115
  • 184
144
votes
22 answers

Difference between Encapsulation and Abstraction

I had an interview today. I had a question from OOP, about the difference between Encapsulation & Abstraction? I replied to my knowledge that Encapsulation is basically binding data members & member functions into a single unit called Class. Whereas…
WpfBee
  • 2,837
  • 6
  • 23
  • 29
125
votes
7 answers

Using a strategy pattern and a command pattern

Both design patterns encapsulate an algorithm and decouple implementation details from their calling classes. The only difference I can discern is that the Strategy pattern takes in parameters for execution, while the Command pattern doesn't. It…
Extrakun
  • 19,057
  • 21
  • 82
  • 129
114
votes
2 answers

What are the differences between the private keyword and private fields in TypeScript?

In TypeScript 3.8+, what are the differences between using the private keyword to mark a member private: class PrivateKeywordClass { private value = 1; } And using the # private fields proposed for JavaScript: class PrivateFieldClass { …
Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
112
votes
5 answers

IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member

I have spent quite a few hours pondering the subject of exposing list members. In a similar question to mine, Jon Skeet gave an excellent answer. Please feel free to have a look. ReadOnlyCollection or IEnumerable for exposing member collections? I…
Marcel De Villiers
  • 1,682
  • 4
  • 15
  • 17
111
votes
27 answers

Good way to encapsulate Integer.parseInt()

I have a project in which we often use Integer.parseInt() to convert a String to an int. When something goes wrong (for example, the String is not a number but the letter a, or whatever) this method will throw an exception. However, if I have to…
Bad Horse
  • 1,113
  • 2
  • 7
  • 4
101
votes
2 answers

C#: Difference between List and Collection (CA1002, Do not expose generic lists)

Tried to run Run Code Analysis on a project here, and got a number of warnings that said something like this: CA1002 : Microsoft.Design : Change 'List' in 'SomeClass.SomeProtectedOrPublicProperty' to use Collection, ReadOnlyCollection or…
Svish
  • 152,914
  • 173
  • 462
  • 620
82
votes
16 answers

Simple way to understand Encapsulation and Abstraction

Learning OOP concepts especially interested to understand Abstraction and Encapsulation in depth. Checked out the below already Abstraction VS Information Hiding VS Encapsulation difference between abstraction and encapsulation? I found very hard to…
Billa
  • 5,226
  • 23
  • 61
  • 105
1
2 3
99 100