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

Advantages of using let over var within a function

Say I have a piece of code like this: const number = 3; function fooFunction() { let numberTwo = 5; var answer = number + numberTwo; return answer; } finalAnswer = fooFunction(); console.log(finalAnswer); Assuming an ES2015 compatible…
Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
7
votes
6 answers

Is inheritance necessary for encapsulation, abstraction and polymorphism?

Today I had an interview for software engineering position. I have read many things from stackoverflow for the interview. They asked me about the normal things realated to OOP. But they also asked me about these: Is Encapsulation possible without…
Muhammad Noman
  • 1,344
  • 1
  • 14
  • 28
7
votes
2 answers

Using Protected Fields in Abstract Class in Java

I'm currently in a Java-based university class and for coding samples the professor is using protected fields for the subclasses to access. I asked if this was bad practice and was told it is normal. Is this true, why not use setters and getters…
Adam Thompson
  • 3,278
  • 3
  • 22
  • 37
7
votes
2 answers

In C++, does adding a friend to a class change its memory layout?

Also, does it matter where in the class you declare the friend ? Does it matter if you add a friend class or a friend function ?
Naveen
  • 5,910
  • 5
  • 30
  • 38
7
votes
4 answers

Property and Encapsulation

Following is a question regarding using properties in class. I have been using public properties instead of exposing member variables publically. Majority advise that this approach helps encapsulation. However, I don’t understand the encapsulation…
Lijo
  • 375
  • 1
  • 4
  • 17
7
votes
6 answers

OOP: Calling a public method within the same class

I was reading some article about collision avoidance systems in cars when my programmer mind led me to think of that concept in the object-oriented way, and it made me wonder if those systems respect the object-oriented programming model. Being…
Alexis Leclerc
  • 1,303
  • 1
  • 16
  • 26
7
votes
1 answer

Android - how to add @hide annotation in my project

I'm developing SDK, and I would like to use @hide annotation for methods/classes which I don't want to be visible for the user who uses my SDK. (same as in Activity implementation - line 3898) I tried just to add @hide annotation but I saw that…
galvan
  • 7,400
  • 7
  • 38
  • 55
7
votes
8 answers

Keep out of reach of Children: Removing protected fields from inheritance

In the spirit of well designed OO, a certain class I am extending has marked one of its fields protected. This class has also generously provided a public setter, yet no getter. I am extending this class with a base class that is in turn extended by…
AnthonyW
  • 1,910
  • 5
  • 25
  • 46
7
votes
11 answers

How do you get people to value abstraction and flexibility over "just getting it done"?

I sometimes have difficulties with other people who wish to solve a problem when they wish to skip the official interfaces and access underlying implementation details directly. They argue that doing so will allow them to solve the problem more…
Andru Luvisi
  • 24,367
  • 6
  • 53
  • 66
7
votes
3 answers

Why not use 'protected' or 'private' in PHP?

I've been working with the Joomla framework and I have noticed that they use a convention to designate private or protected methods (they put an underscore "_" in front of the method name), but they do not explicitly declare any methods public,…
Steven Oxley
  • 6,563
  • 6
  • 43
  • 55
7
votes
6 answers

C# declare both, class and interface

// interface public interface IHasLegs { ... } // base class public class Animal { ... } // derived classes of Animal public class Donkey : Animal, IHasLegs { ... } // with legs public class Lizard : Animal, IHasLegs { ... } // with legs public…
Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
7
votes
4 answers

Should I encapsulate my IoC container?

I'm trying to decide whether or not it makes sense to go through the extra effort to encapsulate my IoC container. Experience tells me that I should put a layer of encapsulation between my apps and any third-party component. I just don't know if…
Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69
7
votes
7 answers

Access private property without get/set

Possible Duplicate: Absence of property syntax in Java See the following situation: class Test extends Object { private int x; public getX() {return x;} public setX(int _x) {x = _x;} } As you can see, nothing special. However, I'd…
user1870263
7
votes
3 answers

Accessing package-private fields in Java

Poking around Android API sources. There's FileDescriptor with a data member descriptor with no access modifier: int descriptor; Then there's class FileOutputStream that constructs a new FileDescriptor and assigns to that field: fd = new…
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
7
votes
5 answers

Linking enum value with localized string resource

Related: Get enum from enum attribute I want the most maintainable way of binding an enumeration and it's associated localized string values to something. If I stick the enum and the class in the same file I feel somewhat safe but I have to assume…
JoeB
  • 2,743
  • 6
  • 38
  • 51