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

Getters/setters of a class having a map

What is the best practice in implementing/providing getters/setters for a class containing a map? The most common implementation I see is: public class MyClass { private Map myMap; public getMyMap() { /* Return an unmodifiable…
jasonline
  • 8,646
  • 19
  • 59
  • 80
12
votes
3 answers

MVC 3, reuse of partial views and jquery, without conflicting the DOM

As i am still new to MVC 3 and jquery, i would like to know a best practice solution to how the following can be solved: I have a view, where I use jquery ajax to fetch and display a partial view with some product details for product A. The loaded…
Nima
  • 937
  • 1
  • 13
  • 20
12
votes
8 answers

How to make a reference type property "readonly"

I have a class Bar with a private field containing the reference type Foo. I would like to expose Foo in a public property, but I do not want the consumers of the property to be able to alter Foo... It should however be alterable internally by Bar,…
Joel in Gö
  • 7,460
  • 9
  • 47
  • 77
12
votes
1 answer

Extending a type in C++

Sadly, UFCS did not make it into C++17 and that left me with a recurring problem: Sometimes I want to give types extra functionality using the method call syntax (without writing global functions). That would especially come handy when dealing with…
Peter Lenkefi
  • 1,306
  • 11
  • 29
12
votes
12 answers

Why stick to get-set and not car.speed() and car.speed(55) respectively?

Apart from unambiguous clarity, why should we stick to: car.getSpeed() and car.setSpeed(55) when this could be used as well : car.speed() and car.speed(55) I know that get() and set() are useful to keep any changes to the data member manageable by…
namespaceform
  • 690
  • 1
  • 6
  • 14
12
votes
8 answers

Why do we pass objects rather than object members to functions?

I have a method FOO() in class A that takes as its arguments input from data members of class B among other things (let's say they are two floats and one int). The way I understand this, it is generally better to implement this with something…
user1790399
  • 220
  • 2
  • 11
11
votes
6 answers

Whats the point of accessing private variables through getter and setter (accessor) functions?

In classes, variables are often made private for encapsulation, and to limit the variables to a certain scope allow better error control and fewer bugs. This makes sense, as the fewer places a variable can be accessed the fewer places a bug can…
fdh
  • 5,256
  • 13
  • 58
  • 101
11
votes
5 answers

A brilliant example of effective encapsulation through information hiding?

"Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through…
11
votes
2 answers

Overloading operator= as Non-Member

According to replies to this thread, operator= cannot be overloaded as a non-member function. So, for example, the following makes the compiler very angry: class MyClass { // ... }; MyClass& operator=(MyClass& Left, MyClass& Right) { //…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
11
votes
3 answers

Advantages to Nested Classes For Listeners in GUIs

For decently sized projects I've been told that when you have classes extending JPanels that the best practice is to use nested classes to implement the listeners. For example I could have a class FactoryScreen that extends JPanel, and have a nested…
11
votes
3 answers

Is there a standard Cyclic Integer Class in C++?

I have a problem that is quite common in the code that I am writing at the moment whereby I want to have an integer that can only exist inside a certain range where the range is [start, end). Basically I want to be able to do something like the…
Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
11
votes
3 answers

How to do encapsulation in Python?

What's wrong with this? From objective, and functional standpoints? import sys class EncapsulationClass(object): def __init__(self): self.privates = ["__dict__", "privates", "protected", "a"] self.protected = ["b"] print…
will
  • 10,260
  • 6
  • 46
  • 69
11
votes
3 answers

Validate a value in property

So I heard that validating a value in a property like this: //dummy example, let's assume that I want my value without dots public string MyProp { set { if(value.Contains('.')) throw new ArgumentException("Must not…
ramires.cabral
  • 910
  • 4
  • 13
  • 28
11
votes
4 answers

C# accessing protected member in derived class

I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); …
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
10
votes
4 answers

Inheritance breaking encapsulation?

People say inheritance breaks encapsulation, which i agree with. They say delegation is better- although the modifiers in delegation can also be public/protected. So is the real reason why inheritance breaks encapsulation because of the "knock-on"…
Jon
  • 185
  • 1
  • 1
  • 8