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
43
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,438
  • 6
  • 23
  • 30
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,165
  • 3
  • 34
  • 71
40
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
  • 37,231
  • 8
  • 113
  • 148
39
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…
39
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,492
  • 11
  • 43
  • 71
37
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
  • 17,293
  • 5
  • 43
  • 66
36
votes
14 answers

What is encapsulation with simple example in php?

What is encapsulation with simple example in php?
Moon
36
votes
3 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
  • 7,637
  • 5
  • 38
  • 40
36
votes
7 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,241
  • 2
  • 21
  • 38
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
  • 707
  • 5
  • 12
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
  • 220,884
  • 228
  • 805
  • 1,125
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,144
  • 7
  • 50
  • 79
32
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,061
  • 3
  • 12
  • 27
31
votes
9 answers

Encapsulation - why do we need it when setters are already public?

Encapsulation is hiding the data. I would like to hear some really interesting answers here. What is the point behind keeping variables as private when we already declare public setter methods for variables? I understand the usage of encapsulation…
Danyal Sandeelo
  • 11,068
  • 6
  • 37
  • 64
29
votes
7 answers

Private Methods Over Public Methods

I was examining the StringTokenizer.java class and there were a few questions that came to mind. I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of…
lbj-ub
  • 1,415
  • 2
  • 16
  • 33