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

Design pattern for multiple output formats

I have a class structure which represents (internally) the data I wish to output to a file. Some of the member variables are private to the data class so that it can manage itself and stop things going awry. I then want this data to be output into…
Dan
  • 12,857
  • 7
  • 40
  • 57
8
votes
6 answers

Acessing the backing field in an auto property

Is there any way to access the backing field for a property in order to do validation, change tracking etc.? Is something like the following possible? If not is there any plans to have it in .NET 4 / C# 4? public string Name { get; set …
Jonathan Parker
  • 6,705
  • 3
  • 43
  • 54
8
votes
2 answers

Is a private synthesized property an oxymoron?

After going through a beginner's iPhone developer book and reading sample code online, I've noticed that most Objective C programmers synthesize nearly every instance variable. Some variables are convenient to snythesize, but most should not when…
JoJo
  • 19,587
  • 34
  • 106
  • 162
8
votes
1 answer

What does 'fidelity' of accessor keywords mean?

I'm reading through .Net Docs and i came across this term "fidelity", Type safety is also used to help enforce encapsulation by guaranteeing the fidelity of the accessor keywords. What does it mean (in relation to accessor keywords)?
chakmeshma
  • 246
  • 8
  • 27
8
votes
3 answers

Program execution is non sequential. Why?

I was fooling around with how I could set up my encapsulation. But my program is executing in an unexpected order. Here is my rather simple code: The "Main": package research.debug; public class Main { public static void main(String[] args) { …
CrazyPenguin
  • 619
  • 2
  • 10
  • 29
8
votes
1 answer

Why does Visual Studio compiler allow violation of private inheritance in this example?

I found very strange behavior of std::unique_ptr in Visual Studio 2013 and 2017. Let's consider an example: class Base { public: virtual ~Base() = default; virtual void Foo() = 0; }; class Derived : private Base { public: void Foo()…
Viktor
  • 1,004
  • 1
  • 10
  • 16
8
votes
1 answer

Is this strategy, to avoid global variables in C, right?

in my (personal) embedded project global variables are piling up. I need those variables to be acessible from the ISR (interrupt service routine) and/or from a menu system (so that they can be modified by the user) but at the same time I'd like to…
zakkos
  • 163
  • 1
  • 11
8
votes
2 answers

Data encapsulation...?

Would anyone be able to explain to me what data encapsulation in Objective-C is? I've been told that this an important concept of Objective-C but I don't see why... Explain it to me as if I was 5 and then as if I was 25.... Thanks for your…
user437038
8
votes
4 answers

How to encapsulate a C API into RAII C++ classes?

Given a C API to a library controlling sessions that owns items, what is the best design to encapsulate the C API into RAII C++ classes? The C API looks like: HANDLE OpenSession(STRING sessionID); void CloseSession(HANDLE hSession); HANDLE…
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
8
votes
6 answers

Why would you mask a base class member?

I have just learned how to mask a base class member (using new) but am missing the point as to why I would want to do that. Does masking provide us with a certain level of protection as is the case in using encapsulation? Please advise.
Asterix
  • 6,005
  • 3
  • 17
  • 12
8
votes
2 answers

In C++, given a member function in class A, can we restrict its access to only class B, without giving B complete friend access to A?

Possible Duplicate: clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom) I've wanted this a couple times and haven't been able to come up with a decent way to do it. Say I've got a member function in class A. I want to be able to…
Kurt Hutchinson
  • 2,959
  • 23
  • 30
8
votes
3 answers

Is it proper to get and especially set Perl module's global variables directly?

I was wondering what the best practice in Perl is regarding getting - or, more importantly, setting - a global variable of some module by directly accessing $Module::varName in case the module didn't provide getter/setter method for it. The reason…
DVK
  • 126,886
  • 32
  • 213
  • 327
8
votes
1 answer

Why am I able to use my value constructor even though I don't export it?

For practice, I'm implementing a queue data type in a module called "Queue". My data type is also called "Queue", as is its only value constructor: module Queue (Queue, enq, emptyQueue) where data Queue a = Queue { inbox :: [a], outbox ::…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
8
votes
2 answers

Command pattern - why encapsulate in an object?

Command pattern is for encapsulating commands in objects. But why not use function pointers instead? Why do I need to subclass Command for each operation? Instead I can have different functions and call the function pointers.
Narek
  • 38,779
  • 79
  • 233
  • 389
8
votes
1 answer

Python object encapsulation security

I have a question, and my decision in choosing Python as a possible language for a bigger project depends on the answer - which I cannot come up with myself: We all know that Python has no real object encapsulation, so there is nothing like…
nerdoc
  • 1,044
  • 10
  • 28