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

How often do you see abuse of C# shorthand getters/setters?

In C# you can create getter/setters in a simpler way than other languages: public int FooBar { get; set; } This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly. My…
dreadwail
  • 15,098
  • 21
  • 65
  • 96
6
votes
5 answers

What is the idea behind private attribute access inside main? Java x C++

When using C++ one is not allowed to access a private attribute inside a main function. Example: #include using namespace std; class Test { private: int a; public: Test(int value) { a = value; } int getValue() {…
Moacir Ponti
  • 591
  • 5
  • 14
6
votes
3 answers

How do I encapsulate included module methods in Ruby?

I want to be able to have methods in a module that are not accessible by the class that includes the module. Given the following example: class Foo include Bar def do_stuff common_method_name end end module Bar def do_stuff …
Chris Aitchison
  • 4,656
  • 1
  • 27
  • 43
6
votes
6 answers

Who do we protect our classes from?

I'm currently learning C# and I was wondering, what is the point of declaring classes / methods private? Who are we hiding / limiting access to these classes. Because if someone was editing the source they could just change the tag from private to…
Hexo
  • 215
  • 4
  • 11
6
votes
7 answers

Why is java secure coding important?

I'm having trouble understanding why java secure coding is important. For example, why is it important to declare variables private? I mean I get that it will make it impossible to access those variables from outside the class, but I could simply…
Anne Onymous
6
votes
2 answers

What is the difference between protected and public variable in python

In python, what is the difference between protected and public variable in a class class A: def __init__(self): self._protected="protected" self.__private="private" self.public="public" >>> a = A() >>>…
6
votes
3 answers

How to achieve encapsulation in C

I am not sure that what I am trying to do is called encapsulation, but it's an OOP concept. I am implementing a binary tree and in particular the insert function: typedef struct __node* tree; typedef struct __node { void* data; tree l,r; }…
6
votes
3 answers

How to ensure that a field is properly encapsulated?

I wonder if there's a simple way to to find all methods accessing a field directly. More precisely: I'd like to assure that there's exactly one method writing a field and exactly one method reading it. All other accesses should use these…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
6
votes
7 answers

What is encapsulation? How does it actually hide data?

Searching turns up a simple definition: data hiding. But, consider the following two examples: 1) First Example: Class Employee { public int age; } 2) Second Example: Class Employee { private int age; public int getAge(){return…
Anil Purswani
  • 1,857
  • 6
  • 35
  • 63
6
votes
3 answers

Make methods/properties visible to one class, hidden to others

I have a class Server which talks to a server connection for IRC. It holds a list of known Users, and creates them as necessary. I have two problems involving the User class: Anyone can create an instance of a User. I only want the Server class…
strager
  • 88,763
  • 26
  • 134
  • 176
6
votes
4 answers

C# "is inaccessible due to its protection level" error in constructor

The constructor of the child class "caesar" gives an error. It says that name, type is inaccessible due to its protection level. How come? As this is a child class derived from "Cipher" class it shouldn't give an error like this. How can I overcome…
Alptugay
  • 1,676
  • 4
  • 22
  • 29
6
votes
3 answers

Encapsulating a large number of parameters in C++14

I want to write a function which uses many parameters, which I will call a, b, and c. I have four choices of implementing this in C++14. For a new modern C++ project in 2018, which one of these styles best adheres to the philosophy of the ISO C++?…
Daniel
  • 510
  • 3
  • 15
6
votes
2 answers

angular 5 removing style node on component destroy

Am I getting this wrong or should style nodes disappear from the head of the document when a component is destroyed? https://github.com/juleskremer/angular/commit/385ed90ac373c0347ea88fe38685405c01ba1a58 If I set encapsulation to "none" the style…
Max Solid
  • 1,213
  • 3
  • 21
  • 32
6
votes
3 answers

Access levels of java class members

I realise that this is a very basic question, but it is one which has always bothered me. As I understand things, if you declare a field private in Java then it is not visible outside of that class. If it is protected then it is available to…
chillysapien
  • 2,256
  • 1
  • 26
  • 42
6
votes
3 answers

C++ overloading and overriding

This code generates the following compilation error : error: no matching function for call to 'C::print(int)' can you help me figuring out the procedure that the compiler did to generate that error , (why it ignored the function in class…