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
19
votes
11 answers

Is OO design's strength in semantics or encapsulation?

Object-oriented design (OOD) combines data and its methods. This, as far as I can see, achieves two great things: it provides encapsulation (so I don't care what data there is, only how I get values I want) and semantics (it relates the data…
Phil H
  • 19,928
  • 7
  • 68
  • 105
19
votes
4 answers

SessionsHelper in railstutorial.org: Should helpers be general-purpose modules for code not needed in views?

railstutorial.org has a suggestion which strikes me as a little odd. It suggests this code: class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper end The include SessionsHelper makes the methods…
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
19
votes
2 answers

What are the differences between the multiple ways to create zero-sized structs?

I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested…
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
19
votes
2 answers

C++ Friend-like construct for Rust

In certain cases I would like to leverage whatever alternative there is in Rust to C++'s friend keyword. In crate A I have the following modules: mod a0: pub struct A { pub a0: u8, a1: SomeType, } impl A { pub fn fa0(...) { ... } fn…
ustulation
  • 3,600
  • 25
  • 50
19
votes
3 answers

C++ private virtual inheritance problem

In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work? class A {}; class B: private virtual A {}; class C:…
Oak
  • 26,231
  • 8
  • 93
  • 152
19
votes
9 answers

C++ Is private really private?

I was trying out the validity of private access specifier in C++. Here goes: Interface: // class_A.h class A { public: void printX(); private: void actualPrintX(); int x; }; Implementation: // class_A.cpp void A::printX() { …
legends2k
  • 31,634
  • 25
  • 118
  • 222
18
votes
5 answers

From [package] import [function] in R

Working with data in Python or R, we often load several packages. In some cases, two packages (e.g. foo and bar) might each contain some function (e.g. do_stuff). The way this is managed in Python to prevent ambiguity or surprises is like: from foo…
Max Power
  • 8,265
  • 13
  • 50
  • 91
18
votes
6 answers

Is it a bad practice to add elements to List using getter method in java?

Suppose I have a private ArrayList or a LinkedList inside a class, that I will never assign new reference to it, or in other words this will never happen: myLinkedList = anotherLinkedList; So that I won't need to use…
Milad R
  • 1,854
  • 9
  • 25
  • 36
18
votes
3 answers

Prefer extension methods for encapsulation and reusability?

edit4: wikified, since this seems to have morphed more into a discussion than a specific question. In C++ programming, it's generally considered good practice to "prefer non-member non-friend functions" instead of instance methods. This has been…
tzaman
  • 46,925
  • 11
  • 90
  • 115
18
votes
5 answers

What is Encapsulation exactly?

I have got two definitions of encapsulation which could not fit into one definition. Encapsulation is data hiding. With the use of private, protected and public, pack the data into single component. Whatever changes encapsulate it. Protecting…
Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291
18
votes
2 answers

Why is it not "inconsistent accessibility" to use a private nested type inside a generic type in the interface list?

In case the title is not completely self-explanatory, here's the code that puzzles me: public interface IFoo { } public class MyClass : IFoo { private class NestedInMyClass { } } I'm suprised this compiles with no…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
18
votes
4 answers

How to integrate a library that uses expression templates?

I would like to use the Eigen matrix library as the linear algebra engine in my program. Eigen uses expression templates to implement lazy evaluation and to simplify loops and calculations. For example: #include int main() { int size…
Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146
17
votes
4 answers

When should I prefer non-member non-friend functions to member functions?

Meyers mentioned in his book Effective C++ that in certain scenarios non-member non-friend functions are better encapsulated than member functions. Example: // Web browser allows to clear something class WebBrowser { public: ... void…
Eric Z
  • 14,327
  • 7
  • 45
  • 69
17
votes
5 answers

Why can't the VBA Me keyword access private procedures in its own module?

I just discovered that the Me keyword cannot access private procedures even when they are inside its own class model. Take the following code in Class1: Private Sub Message() Debug.Print "Some private procedure." End Sub Public Sub…
Kuyenda
  • 4,529
  • 11
  • 46
  • 64
17
votes
2 answers

Shorthand Accessors and Mutators

I am learning C#, and am learning about making fields private to the class, and using Getters and Setters to expose Methods instead of field values. Are the get; set; in Method 1 and Method 2 equivalent? e.g. is one a shorthand of the other? class…
Gravy
  • 12,264
  • 26
  • 124
  • 193