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
24
votes
8 answers

TDD, DDD and Encapsulation

After several years of following the bad practice handed down from 'architects' at my place of work and thinking that there must be a better way, I've recently been reading up around TDD and DDD and I think the principles and practices would be a…
Justin
  • 564
  • 4
  • 13
23
votes
6 answers

Is it worth wrapping a logging framework in an additional layer?

I'm currently looking at upgrading the logging mechanism in a medium-to-large-sized Java codebase. Messages are currently logged using static methods on a Debug class, and I have recommended switching from this to something like SLF4J or…
harto
  • 89,823
  • 9
  • 47
  • 61
22
votes
22 answers

Private vs. Public members in practice (how important is encapsulation?)

One of the biggest advantages of object-oriented programming is encapsulation, and one of the "truths" we've (or, at least, I've) been taught is that members should always be made private and made available via accessor and mutator methods, thus…
Asmor
  • 5,043
  • 6
  • 32
  • 42
22
votes
12 answers

How to access private data members outside the class without making "friend"s?

I have a class A as mentioned below:- class A{ int iData; }; I neither want to create member function nor inherit the above class A nor change the specifier of iData. My doubts:- How to access iData of an object say obj1 which is an instance…
Abhineet
  • 6,459
  • 10
  • 35
  • 53
22
votes
2 answers

Do we need to wrap ES6 code in an IIFE?

In ES5, writing such code has been considered as good practice: (function () { //some magic })(); But in ES6 variables created with let keyword are not attached to window object. So, is there any need now in writing our code in an IIFE, or it…
22
votes
3 answers

Idiomatic encapsulation in Clojure: How can data be bundled with linked behavior?

I'm trying to figure out whether Clojure is something that can fully replace the paradigms that I'm used to in other languages. One thing that I don't understand is how to idiomatically achieve encapsulation in Clojure (by encapsulation I'm…
Kzqai
  • 22,588
  • 25
  • 105
  • 137
22
votes
9 answers

Java encapsulation

We always say that data will be encapsulated if we simply define variables private and define getters setters to access those variables. My question is if we can access the variables (data) though via getters and setters, how come data is hidden or…
Just_another_developer
  • 5,737
  • 12
  • 50
  • 83
21
votes
8 answers

C++ Header file that declares a class and methods but not members?

Is it possible to make a C++ header file (.h) that declares a class, and its public methods, but does not define the private members in that class? I found a few pages that say you should declare the class and all its members in the header file,…
David Grayson
  • 84,103
  • 24
  • 152
  • 189
21
votes
6 answers

Apply CSS dynamically with JavaScript

What is a good way to apply styling dynamically (i.e. the value of styles are created at runtime) to HTML elements with JavaScript? I'm looking for a way to package a JavaScript widget (JS, CSS and markup) in a single component (basically, an…
Juan Carlos Coto
  • 11,900
  • 22
  • 62
  • 102
21
votes
3 answers

When would you use the "protected internal" access modifier?

As you may already know, the .NET Framework's protected internal access modifier works in a strange way: It doesn't mean the class is protected AND internal, it says the class is protected OR internal; that is, the modified class or member can be…
Pablo Marambio
  • 1,562
  • 1
  • 15
  • 29
21
votes
7 answers

Can I write validation logic in setter methods?

Are setter methods only used to set the value of attributes as it is passed as argument? Can we write some validation logic before assigning the value to the attributes?
Mac
  • 1,711
  • 3
  • 12
  • 26
20
votes
6 answers

Is it okay to forgo getters and setters for simple classes?

I'm making a very simple class to represent positions in 3D space. Currently, I'm just letting the user access and modify the individual X, Y and Z values directly. In other words, they're public member variables. template
Maxpm
  • 24,113
  • 33
  • 111
  • 170
20
votes
10 answers

How to sort a list by a private field?

My entity class looks like this: public class Student { private int grade; // other fields and methods } and I use it like that: List students = ...; How can I sort students by grade, taking into account that it is a private…
Fanta
  • 248
  • 2
  • 8
20
votes
1 answer

Why does the type System.__ComObject claim (sometimes) to be public when it is not?

Just an oddity I happened to discover when I was reflecting over all types to check something else out of curiosity. Why does the class System.__ComObject of the assembly mscorlib.dll (sometimes?) claim to be public when in fact it seems to be…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
20
votes
2 answers

Cannot provide arguments when creating an instance of generic type

I have an object that I want to have read only after it is created... namely because the properties in the constructor must be used in GetHashCode, and therefore can't change once created. I this is one of many classes that are readonly: public…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448