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)

1900 questions
0
votes
1 answer

Android: delete from database using index from listview - Can't access array of id's that is created with the view but not the basis for the list

I have a simple list view where each item is a view that has a title, from an ArrayList of strings and button, so that each entry in the ArrayList creates a new list item. I also have another ArrayList of corresponding primary keys, which I want…
quixo
  • 3
  • 2
0
votes
2 answers

How does encapsulation works in OOP

public interface IShoulders { public void move(); } public class Body { public void createBody(){ RightHand rightHand = new RightHand(); rightHand.move(); IShoulders leftHand = new LeftHand(); leftHand.move(); } } public…
xtabbas
  • 567
  • 2
  • 8
  • 18
0
votes
3 answers

how to use getter and setter for Encapsulation

I have a confusion in how do getter and setter provide encapsulation. I mean what is the difference between assigning value to a variable directly and assigning them through getter and setter. Also lets say we have two class A & B. While using…
Pratik Paul
  • 83
  • 1
  • 10
0
votes
1 answer

Cannot pass variable to object in c# due to protection level

I've got this code, and trying to pass two parameters to LatLng (if hardcoded it works fine), but keep getting is inaccessible due to its protection level - on var location = new LatLng... What is wrong with my encapsulation? namespace SomeApp { …
Mac_W
  • 2,251
  • 1
  • 14
  • 28
0
votes
1 answer

Using reference variables or send them as function parameters

I have the next class: class State { public: State(StateMachine& machine, sf::RenderWindow& window) : mMachine { machine } , mWindow { window } { } void draw(); protected: StateMachine& mMachine; …
Mihai
  • 229
  • 1
  • 10
0
votes
2 answers

Need of private modifier in C#

I am new to C#.This makes me confusing.If we building some project.Why we have private data members.Although everything is accessed by us according to our requirement.Nobody have access to our code.Then what is the need of private modifiers in C#
0
votes
3 answers

Is encapsulation in Swift as important as in other OO languages

When I was learning other languages such as Java, all the sample code tells you that you should always mark a field private. And the methods that do the internal stuff are also marked private or protected. But when it comes to Swift, I see that the…
Sweeper
  • 145,870
  • 17
  • 129
  • 225
0
votes
2 answers

confused about inheritance in c++ (public and private)

#include #include using namespace std ; enum COLOR { Green, Blue, White, Black, Brown } ; class Animal { public : Animal() : _name("unknown") { cout << "constructing Animal object "<< _name << endl ; } …
Kakayou
  • 378
  • 3
  • 15
0
votes
4 answers

In C++ is it possible to call an accessor through property-like syntax?

I am working with a large code base, and there are a number of publicly defined variables. Unfortunately, the functions of accessing these variables has changed, and this new functionality would be best encapsulated by public accessors and a…
Veita
  • 499
  • 1
  • 5
  • 16
0
votes
0 answers

Will access specifiers help to achieve abstraction?

I think, this question is going to be a duplicate. But after lot of thinking and searching also, I am unable clear this question in mind. Now coming to my question, It will be better if I see clear YES or NO answers to the below question 1) Does…
0
votes
1 answer

Should I have redundant method names between related classes?

I'm building a sports product. I have 3 classes class Team { getName // ex: Los Angeles Lakers getShortName // ex: Lakers getAbbrName // ex: LAL } class Match { Team getHomeTeam Team getAwayTeam Play[] plays } class Play { Match…
Popcorn
  • 4,710
  • 10
  • 45
  • 80
0
votes
1 answer

Encapsulating `sort` Interface in Golang

I am trying to sort a slice of structs in Go. I can implement the sort.Interface by defining 3 methods at the top level of the package: type byName []*Foo // struct Foo is defined in another package func (a byName) Len() int { return…
Ralph
  • 29,142
  • 31
  • 124
  • 261
0
votes
0 answers

C# passing canvas from XAML to another class not using constructor

Is there any way to pass a canvas not directly form MainWindow class to another class not using constructor or setter? What I mean is: class MainWindow{ public MainWindow(){ // here we got our canvas property } } public interface IDraw{ …
Berrigan
  • 420
  • 4
  • 21
0
votes
2 answers

Design pattern: inheritance and encapsulated inheritance

I got problems formulating it precisely so I left more general description in the title (if you have more precise description of the problem, please comment, I'll edit the title). The problem: Two classes AudioStream and VideoStream are derived from…
peetonn
  • 2,623
  • 4
  • 23
  • 37
0
votes
4 answers

Safe/Correct use of pointers

I understand that you shouldn't return a pointer or reference to member data because it breaks encapsulation. But I'm not sure if that is what's happening in this case and I could use some clarification... Tree header- #include "TreeNode.h" class…
LBaelish
  • 579
  • 1
  • 9
  • 20
1 2 3
99
100