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

What are the differences between information hiding and encapsulation?

What are the differences between information hiding and encapsulation? I have read that encapsulation means bundling data and the procedures that should operate on them together. If that is so, does the following class achieve encapsulation? class…
user1720616
  • 543
  • 1
  • 6
  • 13
32
votes
9 answers

Encapsulation - why do we need it when setters are already public?

Encapsulation is hiding the data. I would like to hear some really interesting answers here. What is the point behind keeping variables as private when we already declare public setter methods for variables? I understand the usage of encapsulation…
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
29
votes
7 answers

Private Methods Over Public Methods

I was examining the StringTokenizer.java class and there were a few questions that came to mind. I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of…
lbj-ub
  • 1,425
  • 2
  • 19
  • 34
28
votes
5 answers

Why encapsulation is an important feature of OOP languages?

I came across different interview where question was asked to me why encapsulation is used? Whose requirement actually is encapsulation? Is it for users of program? Or is it for co-workers? Or is it to protect code from hackers?
Shahzad
  • 550
  • 1
  • 6
  • 24
28
votes
2 answers

Globally defined AngularJS controllers and encapsulation

According to AngularJS's tutorial, a controller function just sits within the global scope. http://docs.angularjs.org/tutorial/step_04 Do the controller functions themselves automatically get parsed into an encapsulated scope, or do they dwell…
Andrew Rhyne
  • 5,060
  • 4
  • 28
  • 41
27
votes
2 answers

Why can private member variable be changed by class instance?

class TestClass { private string _privateString = "hello"; void ChangeData() { TestClass otherTestClass = new TestClass(); otherTestClass._privateString = "world"; } } This code compiles in C# and the equivalent…
Michael Low
  • 24,276
  • 16
  • 82
  • 119
27
votes
5 answers

Public and Privileged methods in javascript: Why are they called that way?

If I understand correctly, according to Douglas Crockford http://javascript.crockford.com/private.html, the "privileged" methods are similar to what we know as "public" methods. and "public" methods are something that's a bit different. Here's how I…
Vlad
  • 8,038
  • 14
  • 60
  • 92
26
votes
4 answers

Pattern for Creating a Simple and Efficient Value type

Motivation: In reading Mark Seemann’s blog on Code Smell: Automatic Property he says near the end: The bottom line is that automatic properties are rarely appropriate. In fact, they are only appropriate when the type of the property is a value…
ErnieL
  • 5,773
  • 1
  • 23
  • 27
26
votes
3 answers

Doctrine2 ORM does not save changes to a DateTime field

I have a User entity: use Doctrine\ORM\Mapping as ORM; /** * ExampleBundle\Entity\User * * @ORM\Entity() */ class User { // ... /** * @ORM\Column(type="service_expires_at", type="date", nullable=true) */ private…
Laurynas Mališauskas
  • 1,909
  • 1
  • 19
  • 34
25
votes
2 answers

Why would you declare getters and setters method private?

I saw a code where getters and setters methods are declared private. I am trying to figure out the logic behind it, and I am really having hard time to understand why would you declare them as private? That's exactly opposite of what we are trying…
Vandan Patel
  • 273
  • 1
  • 3
  • 5
25
votes
7 answers

C# marking member as "do not use"

public class Demo { private List _items; private List Items { get { if (_items == null) _items = ExpensiveOperation(); return _items; } } } Other…
nivlam
  • 3,223
  • 4
  • 30
  • 39
25
votes
3 answers

SQL Server: How to permission schemas?

Inspired by various schema related questions I've seen... Ownership chaining allows me to GRANT EXECUTE on a stored procedure without explicit permissions on tables I use, if both stored procedure and tables are in the same schema. If we use…
gbn
  • 422,506
  • 82
  • 585
  • 676
25
votes
12 answers

Java Encapsulation Concept not clear

This is basic question but still i don't understand encapsulation concept . I did't understand how can we change the properties of class from other class.because whenever we try to set the public instance value of class we have to create object of…
user2693404
  • 307
  • 1
  • 3
  • 9
25
votes
2 answers

Is there a way to declare public and private methods for S4 Reference Classes?

Up-front: I am aware that R is a functional language, so please don't bite ;-) I've had great experiences with using an OOP approach for a lot of my programs. Now, I'm wondering if there's a way to make a distinction between public and private…
Rappster
  • 12,762
  • 7
  • 71
  • 120
24
votes
4 answers

How can I expose iterators without exposing the container used?

I have been using C# for a while now, and going back to C++ is a headache. I am trying to get some of my practices from C# with me to C++, but I am finding some resistance and I would be glad to accept your help. I would like to expose an iterator…
Statement
  • 3,888
  • 3
  • 36
  • 45