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
47
votes
7 answers

Dependency Inversion Principle (SOLID) vs Encapsulation (Pillars of OOP)

I was recently having a debate about the Dependency Inversion Principle, Inversion of Control and Dependency Injection. In relation to this topic we were debating whether these principles violate one of the pillars of OOP, namely Encapsulation. My…
45
votes
7 answers

Effective C++ Item 23 Prefer non-member non-friend functions to member functions

While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer non-member non-friend functions to member functions. Reading that at first…
44
votes
14 answers

What is encapsulation with simple example in php?

What is encapsulation with simple example in php?
Moon
44
votes
12 answers

Why is the amount of visibility on methods and attributes important?

Why shouldn't one leave all methods and attributes accessible from anywhere (i.e. public)? Can you give me an example of a problem I can run into if I declared an attribute as public?
tirenweb
  • 30,963
  • 73
  • 183
  • 303
42
votes
12 answers

Properties vs. Fields: Need help grasping the uses of Properties over Fields

First off, I have read through a list of postings on this topic and I don't feel I have grasped properties because of what I had come to understand about encapsulation and field modifiers (private, public..ect). One of the main aspects of C# that I…
pghtech
  • 3,642
  • 11
  • 48
  • 73
42
votes
5 answers

How to implement C# access modifiers in javascript?

Summary I tried to achieve inheritance and encapsulation properly in javascript like it was in a class-based language such as c#. The ugly part is the protected members have multiple copies in the private instances which are only accessible via…
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
40
votes
8 answers

accessing a protected member of a base class in another subclass

Why does this compile: class FooBase { protected: void fooBase(void); }; class Foo : public FooBase { public: void foo(Foo& fooBar) { fooBar.fooBase(); } }; but this does not? class FooBase { protected: void…
Kaiserludi
  • 2,434
  • 2
  • 23
  • 41
39
votes
3 answers

Ruby class with static method calling a private method?

I have a class with a number of static methods. Each one has to call a common method, but I'm trying not to expose this latter method. Making it private would only allow access from an own instance of the class? Protected does not seem like it…
yamori
  • 1,213
  • 4
  • 14
  • 27
38
votes
2 answers

Why JUnit 5 default access modifier changed to package-private

Why is the default access modifier in JUnit 5 package-private? Tests in JUnit 4 had to be public. What is the benefit of changing it to package-private?
wojtek1902
  • 503
  • 2
  • 10
  • 25
38
votes
1 answer

C++ compilers diverge in encapsulation behavior - which one gets it right?

Compilers (clang-5.0.0, GCC-7.3, ICC-18 and MSVC-19) diverge w.r.t. accessibility of members of A below. class A { template static constexpr int f() { return 0; } template struct B {}; template using C =…
Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
35
votes
14 answers

What good are public variables then?

I'm a total newbie with tons of ?'s in my mind and a lot to experience with C++ yet! There's been something which I find really confusing and it's the use of public variables, I've seen tons of code like this: class Foo { private: int…
Parsa Jamshidi
  • 717
  • 5
  • 13
35
votes
9 answers

How to use Dependency Injection without breaking encapsulation?

How can i perform dependency injection without breaking encapsulation? Using a Dependency Injection example from Wikipedia: public Car { public float getSpeed(); } Note: Other methods and properties (e.g. PushBrake(), PushGas(), …
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
35
votes
13 answers

Getters and Setters are bad OO design?

Getters and Setters are bad Briefly reading over the above article I find that getters and setters are bad OO design and should be avoided as they go against Encapsulation and Data Hiding. As this is the case how can it be avoided when creating…
Julio
  • 6,182
  • 11
  • 53
  • 65
34
votes
17 answers

What is the use of encapsulation when I'm able to change the property values with setter methods?

I try to understand a lot of times but I failed to understand this. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be…
PSR
  • 39,804
  • 41
  • 111
  • 151
33
votes
7 answers

Any reason to use auto-implemented properties over manual implemented properties?

I understand the advantages of PROPERTIES over FIELDS, but I feel as though using AUTO-implemented properties over MANUAL implemented properties doesn't really provide any advantage other than making the code a little more concise to look at it. I…
CptSupermrkt
  • 6,844
  • 12
  • 56
  • 87