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
17
votes
3 answers

Can you explain this thing about encapsulation?

In response to What is your longest-held programming assumption that turned out to be incorrect? question, one of the wrong assumptions was: That private member variables were private to the instance and not the class. (Link) I couldn't catch…
Moayad Mardini
  • 7,271
  • 5
  • 41
  • 58
17
votes
3 answers

Can I use private instance methods as callbacks?

My particular scenario involves doing some text transformation using regular expressions within a private method. The private method calls preg_replace_callback, but is seems that callbacks need to be public on objects, so I'm stuck breaking out of…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
17
votes
6 answers

Non-member vs member functions in Python

I'm relatively new to Python and struggling to reconcile features of the language with habits I've picked up from my background in C++ and Java. The latest issue I'm having has to do with encapsulation, specifically an idea best summed up by Item…
JimmidyJoo
  • 10,503
  • 7
  • 27
  • 30
16
votes
5 answers

What is the visibility of @synthesized instance variables?

If you have a property in your public interface like the following @interface MyClass : NSObject @property(strong) NSString *myProp; @end And then synthesize it, in effect synthesizing the variable: @implementation MyClass @synthesize myProp =…
jbrennan
  • 11,943
  • 14
  • 73
  • 115
16
votes
13 answers

Isn't there a point where encapsulation gets ridiculous?

For my software development programming class we were supposed to make a "Feed Manager" type program for RSS feeds. Here is how I handled the implementation of FeedItems. Nice and simple: struct FeedItem { string title; string description; …
y2k
  • 65,388
  • 27
  • 61
  • 86
15
votes
2 answers

DDD and the use of Getters and Setters

I've read a few articles/posts regarding the use of Getters and Setters, and how they help to defeat the purpose of encapsulation in domain model objects. I understand the logic behind not using setters - you are allowing client code to manipulate…
Chris
  • 7,996
  • 11
  • 66
  • 98
15
votes
3 answers

How deal with warning "Access to protected member" in pycharm method?

I have some class class A(object): def __init__(self, data): self._data = data def _equals(self, other): return self._data == other._data Pycharm doesn't like that I access other._data because it is private. "Access to…
Gulzar
  • 23,452
  • 27
  • 113
  • 201
15
votes
5 answers

Why do people write private-field getters returning a non-const reference?

We can all agree on public variables being bad for encapsulation and all that. However, I noticed a lot of code that does this type of stuff: class foo { private: int integer_; string someString_; // other variables public: int&…
Everyone
  • 1,751
  • 13
  • 36
15
votes
1 answer

Abstracting away from data structure implementation details in Clojure

I am developing a complex data structure in Clojure with multiple sub-structures. I know that I will want to extend this structure over time, and may at times want to change the internal structure without breaking different users of the data…
mikera
  • 105,238
  • 25
  • 256
  • 415
15
votes
2 answers

respond_to? and protected methods

It may not be so obvious how respond_to? works in ruby. Consider that: class A def public_method end protected def protected_method end private def private_method end end obj = A.new obj.respond_to?(:public_method) #…
mlomnicki
  • 167
  • 1
  • 10
15
votes
6 answers

I can't create a clear picture of implementing OOP concepts, though I understand most of the OOP concepts. Why?

I have been working on some of the projects of my own and dont have any indrustial exposure. Currently i use simple approach for developing small applications with negligible OO approach like creating a common class for database functions using…
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
14
votes
10 answers

Doesn't Spring's dependency injection break information hiding?

Coming from a C++ background I have to master the complexity of the Java world and its frameworks. Looking at the spring framework for DI I am finding it difficult to believe that I have to make each setter function which will be subject for DI…
struppi
  • 595
  • 1
  • 3
  • 15
14
votes
2 answers

Where to draw the line between Clojure and Java?

I have an interesting architectural question regarding an application that I am developing using both Clojure and Java. The application involves a lot of intensive, concurrent data processing tasks that need to be orchestrated. Here's the rationale…
mikera
  • 105,238
  • 25
  • 256
  • 415
14
votes
9 answers

How do you take decision to define a variable "private"?

I have attended a job interview. The interviewer asked me why you need private variable. If you achieve something by defining a variable private, can't you achieve the same by defining any other access modifier defined in java? According to Java…
Shashi
  • 12,487
  • 17
  • 65
  • 111
14
votes
2 answers

Allowing users of a class to move private members

Say I have this class: class Message { public: using Payload = std::map; Message(int id, Payload payload) : id_(id), payload_(std::move(payload)) {} int id() const {return id_;} const…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122