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

golang public method to private struct - is this have any use case

I am new to go. I noticed in some libraries there are public methods to a private struct. See for example https://github.com/btcsuite/btcd/blob/master/txscript/stack.go Does this have a real use case? I cannot picture how I will be able to use…
gsf
  • 6,612
  • 7
  • 35
  • 64
9
votes
1 answer

Declaring private member variables

I've started learning Objective-C a few weeks ago and I still don't understand how to manage the encapsulation of a class correctly. What is the best way to declare a private member variable in a class? It seems that setting the right getter/setter…
9
votes
1 answer

How to find variables defined outside current scope in C#?

I'm using Visual Studio 2013. I'm reworking some horrible code left by someone else, which uses almost EXCLUSIVELY global variables, and trying to clean that up so I can have each function be properly encapsulated and not cause effects outside of…
eidylon
  • 7,068
  • 20
  • 75
  • 118
9
votes
5 answers

Why protected method is not accessible from subclass?

Consider the following code snippets: package vehicle; public abstract class AbstractVehicle { protected int speedFactor() { return 5; } } package car; import vehicle.AbstractVehicle; public class SedanCar extends AbstractVehicle…
Hariharan
  • 881
  • 1
  • 13
  • 25
9
votes
5 answers

Is it a good practice to use package private methods in order to facilitate unit tests?

Some times I found myself in situations in which unit tests would be easier if I change the visibility of some methods from private to package private in order to either facilitate unit test mocking, assertions... One example would be this Say I…
Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
9
votes
6 answers

Why public event cannot be invoked outside directly?

Consider we have a class with event declared: public class FooBar { public event EventHandler FooBarEvent; } Despite of "publicness" of the event, we cannot call FooBarEvent.Invoke from outside. This is overcame by modyfing a class with the…
Ilya Tereschuk
  • 1,204
  • 1
  • 9
  • 21
9
votes
10 answers

Do these two classes violate encapsulation?

class X { protected: void protectedFunction() { cout << "I am protected" ; } }; class Y : public X { public: using X::protectedFunction; }; int main() { Y y1; y1.protectedFunction(); } This way I am able to expose one of the…
Yogesh Arora
  • 2,216
  • 2
  • 25
  • 35
9
votes
8 answers

How to encapsulate an array in Java

I'm starting with Java and I'm learning about setters, getters and encapsulation. I have a very simple program, two classes: Container has a private int array (numArray) with his setter & getter. Main creates a Container object and uses it in…
Setwarn
  • 93
  • 1
  • 1
  • 4
9
votes
4 answers

Dosen't Reflection API break the very purpose of Data encapsulation?

Very recently I came across the Reflection API and to my surprise we can access and even alter the private variables.I tried the following code import java.lang.reflect.Field; public class SomeClass{ private String name = "John"; } public…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
9
votes
1 answer

How Buffered Streams works?

In Java and C# there are several classes for buffering streams: BufferedStream in C#, Buffered(Input|Output)Stream and Buffered(Reader|Writer). They gets some stream in constructor and implements the same interface. The question is - how it…
VorobeY1326
  • 782
  • 10
  • 25
9
votes
2 answers

Public variables in Python classes?

I am learning Python classes on my own right now and came across this page: http://www.tutorialspoint.com/python/python_classes_objects.htm The variable empCount is a class variable whose value would be shared among all instances of a this class.…
John Smith
  • 11,678
  • 17
  • 46
  • 51
9
votes
2 answers

C# protected field access

(This question is a follow-up to C# accessing protected member in derived class) I have the following code snippet: public class Fox { protected string FurColor; private string furType; public void PaintFox(Fox anotherFox) { …
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
9
votes
6 answers

Do Subclasses Inherit Private Instance Variables From Superclasses

Do subclasses inherit private fields? This question addresses the same problem but I don't quite understand how that satisfies the (seemingly) contradictory situations below. http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Says…
Ian
  • 561
  • 4
  • 8
  • 15
8
votes
8 answers

Why encapsulate this field?

I've always been told to encapsulate ANY and ALL properties from a class... This is right: private string propertyName; public string PropertyName { get { return propertyName; } set { propertyName = value; } } And this is WRONG Public…
8
votes
3 answers

Is adding attributes dynamically frowned upon in Python?

In Python, you can assign an arbitrary attribute from outside the defining class: class Profile(models.Model): user = models.OneToOneField(User) name = models.CharField(max_length=140) p = Profile() p.age = 42 The underlying mechanism…
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511