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)

1900 questions
1087
votes
16 answers

Difference between private, public, and protected inheritance

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
710
votes
13 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
  • 10,421
  • 5
  • 26
  • 22
371
votes
30 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
  • 6,461
  • 8
  • 36
  • 45
366
votes
39 answers

Difference between abstraction and encapsulation?

What is the precise difference between encapsulation and abstraction?
Rocky
  • 3,671
  • 3
  • 16
  • 5
226
votes
4 answers

Understanding the difference between __getattr__ and __getattribute__

I am trying to understand the difference between __getattr__ and __getattribute__, however, I am failing at it. The answer to the Stack Overflow question Difference between __getattr__ vs __getattribute__ says: __getattribute__ is invoked before…
user225312
  • 108,033
  • 64
  • 161
  • 179
176
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
  • 11,420
  • 14
  • 40
  • 36
174
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
  • 237,063
  • 85
  • 359
  • 638
164
votes
9 answers

Java: Subpackage visibility?

I have two packages in my project: odp.proj and odp.proj.test. There are certain methods that I want to be visible only to the classes in these two packages. How can I do this? EDIT: If there is no concept of a subpackage in Java, is there any way…
Nick Heiner
  • 108,809
  • 177
  • 454
  • 689
133
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
  • 13,659
  • 20
  • 88
  • 156
121
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
  • 18,259
  • 19
  • 74
  • 122
103
votes
18 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 her to my knowledge that Encapsulation is basically to bind data members & member functions into a single unit called Class.…
WpfBee
  • 2,307
  • 6
  • 20
  • 26
99
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
  • 138,188
  • 158
  • 423
  • 589
93
votes
25 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
  • 933
  • 1
  • 6
  • 4
78
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-Is-Hier
  • 1,297
  • 3
  • 11
  • 17
77
votes
13 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,227
  • 3
  • 12
  • 17
1
2 3
99 100