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
6 answers

what is the advantage of using private variables in C#

Sample code (alternative code is below), // person.cs using System; class Person { private string myName ="N/A"; // Declare a Name property of type string: public string Name { get { return myName; …
michael
  • 661
  • 1
  • 7
  • 18
8
votes
2 answers

Clojure allows encapsulation and inheritance, but can I combine them?

Here is an overly simplistic example for illustration: I can encapsulate an implementation detail such as using an atom for a counter: (defn make-counter ([] (make-counter 0)) ([init-val] (let [c (atom init-val)] {:get (fn [] @c) …
Timothy Pratley
  • 10,586
  • 3
  • 34
  • 63
7
votes
4 answers

How can I prevent a third party from calling certain methods?

I have an assembly which is being developed to create a facade around some potentially sensitive functionality and I want to allow a third party to call some of the methods contained within it but not others. How can I prevent the third party…
Ant Swift
  • 20,089
  • 10
  • 38
  • 55
7
votes
7 answers

Do objects encapsulate data so that not even other instances of the same class can access the data?

In Java, Do objects encapsulate data so that not even other instances of the same class can access the data? Only when the keyword "private" is used? What are "accessor methods" in Java - methods like getName()? Thanks
Devoted
  • 177,705
  • 43
  • 90
  • 110
7
votes
1 answer

Exposing protected constructor in parent class

As the title says, I'm wondering if there's a way to expose a protected constructor in a derived class (that is, to change access from protected to public). Consider the following (contrived) example: struct A { int a; int b; protected: …
YiFei
  • 1,752
  • 1
  • 18
  • 33
7
votes
5 answers

Objective-c: why private ivars are not hidden from the outside access when using KVC

After trying to access ivars using KVC, I have noticed that there was no protection on private and protected ivars. It doesn't matter what I put a in front of the ivar (private or protected keyword) - an ivar is always a public ivar when using KVC…
Centurion
  • 14,106
  • 31
  • 105
  • 197
7
votes
4 answers

C# Design Problem Regarding Data Encapsulation

I have four classes: 1: one that owns the data 2: another that updates the data 3: third that is informed by the first about certain changes of the data 4: last that reads certain properties from the first class I do not want any other class but the…
geeko
  • 2,649
  • 4
  • 32
  • 59
7
votes
3 answers

Encapsulation in the age of frameworks

At my old C++ job, we always took great care in encapsulating member variables, and only exposing them as properties when absolutely necessary. We'd have really specific constructors that made sure you fully constructed the object before using…
Andy White
  • 86,444
  • 48
  • 176
  • 211
7
votes
28 answers

In Java, is it ever a bad idea to make an object's members publicly available?

I have a data class in my application. My application will never be used as a public API and I will be the only person developing code within my project. I am trying to save every ounce of processor and memory power I can. Is it a bad idea to…
jbu
  • 15,831
  • 29
  • 82
  • 105
7
votes
3 answers

Is Fragile Base Class the only reason why "inheritance breaks encapsulation"?

As the Gang of Four states it in "Design Patterns": "it's often said that 'inheritance breaks encapsulation'", paraphrasing Snyder in "Encapsulation and Inheritance in Object-Oriented Programming Languages". However, each time I read that…
7
votes
2 answers

Are there performance benefits to Angular's component style encapsulation?

I've been reading about Angular 2's style encapsulation methods where you can write your styles directly into the component. This method can use the native shadow dom or an emulated one. What are the performance benefits to using either for…
Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
7
votes
2 answers

Do I really need to bend over backwards for a friend operator<< for a class in a namespace?

I want to implemnent an operator<< for streaming my class (say it's named Paragraph). Class Paragraph has some private data, for which reason I want the (freestanding) operator<< function to be a friend. So, I do as suggested, e.g., here on SO.…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
2 answers

Scala type alias including companion object [beginner]

I'd like to write a type alias to shorten, nice and encapsulated Scala code. Suppose I got some collection which has the property of being a list of maps, the value of which are tuples. My type would write something like List[Map[Int, (String,…
Jean
  • 10,545
  • 2
  • 31
  • 31
7
votes
2 answers

Why does Swift allow access of private variables in the same file?

I've just found out that Swift's private access modifier is file level, as stipulated in the docs under "Access Levels": Private access in Swift differs from private access in most other languages, as it’s scoped to the enclosing source file rather…
Allen Zeng
  • 2,635
  • 2
  • 20
  • 31
7
votes
7 answers

Data encapsulation in C# using properties

currently I am thinking about data encapsulation in C# and I am a little bit confused. Years ago, when I started to learn programing with C++, my professor told me: - "Create a class and hide it data members, so it can't be manipulated directly from…
Ferhat
  • 471
  • 3
  • 9
  • 21