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.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
38 views

Encapsulation problem when using interfaces in C#

Case: Imagine that we developing some library (.dll) with two classes: public Employee: IEmployee, IPerson and public MagicManager that acts as data base implementation for example. The Employee class ...
  • 19
-3 votes
0 answers
41 views

Can this class cause unexpected behavior? [closed]

I came across an example of a class that seems to be designed to represent a generic shape object in 2D space. This is the base class for the triangle, line, rectangle, and trapezoid classes. These ...
  • 1
1 vote
2 answers
42 views

In C# if it recommended that I always declare my instance fields private (encapsulation) should I also always declare my static fields as private?

public class Person { private string _name; // always recommended to be private private static string s_homePlanet; // recommended? } As mentioned in the question the practice of ...
  • 43
1 vote
1 answer
48 views

How do you fix this privacy leak?

I am using java for this code. I created a class that has two private variables. I have a constructor that takes two dates as arguments, a copy constructor, a default constructor and a setter for each ...
  • 11
0 votes
1 answer
22 views

Need help to verify own interpretation of the main purpose of the encapsulation principe

When I initially acknowledge with the encapsulation it seems I got the wrong feeling that the main reason of the encapsulation is to prevent modifying class data by object user and hide the internal ...
-3 votes
0 answers
35 views

What does the set keyword actually do?

I understand that this is a basic question but I cannot find anywhere online that specifies what "set" actually does. I have been using it along side the get keyword for the encapulisation ...
0 votes
1 answer
29 views

django rest framework serializer, create an object to encapsulate some model field

I have a serializer which looks like this: class ListingSerializer(serializers.ModelSerializer): class Meta: model = Listing fields = '__all__' My Listing model have some field: ...
  • 3,463
0 votes
0 answers
23 views

How can I make a generic receive class only use the generic broadcast interface of the corresponding type

A state machine for war chess. I want to make events such as moving (button and mouse dragging) and releasing skills into states. Other chess will grasp the reality and do their work. I didn't use ...
0 votes
2 answers
46 views

How can I encapsulate my code, so I can pass arguments from one function to another?

I wrote this simple carousel but without encapsulation. So previously I placed items from buttonControl() in global scope and added eventListeners on global scope, which are now emraced in prev() and ...
  • 1
0 votes
1 answer
43 views

Change emulation back when navigating off page - angular javascript

I have the following code in my component.ts file on one particular page, because that page needs to use it's own css for when the page is printed: @Component({ selector: "dashboard", ...
user avatar
0 votes
0 answers
25 views

How Inheritance Weakens Encapsulation

In the book The object oriented thought process it is stated that: Encapsulation is the process of packaging classes into the public interface and the private implementation. Example: ---------------...
  • 139
0 votes
0 answers
9 views

Trouble with encapsulation - "Cannot be resolved to a variable" error [duplicate]

I'm trying to use encapsulation to assign values to variables via user entry, but my set methods aren't recognizing my variables. My code is as follows: static class Sneakers{ //Declare variables ...
1 vote
1 answer
68 views

Composition over inheritance, sure, but do we have any syntactic sugar for the function passthrough boilerplate?

If I have a struct implementing a trait, and then I encapsulate that struct in another struct, is there an easy way to pass through all the function calls to implement that trait for the second struct?...
0 votes
1 answer
35 views

Cannot able to find output for Getters and setters in Map <LinkedHashMap>? [duplicate]

I have created 2 classes [Banking and Website] In banking class I have created Getters and setters for username and password data In Website class I have created LinkedHashMap to get the output. But ...
0 votes
1 answer
51 views

Can I reach performance if I encapsulate a parameter of a recursive function?

I have a function that sorts a given array using the merge sort algorithm. That algorithm calls itself to solve the problem. public class MergeSort { public static void mergeSort(int[][] arr, ...
  • 33
0 votes
2 answers
146 views

Jetpack compose mutableState and encapsulation

I have seen examples in the android official documentation that exposing property like that var showDialog = mutableStateOf(false) private set but i didn't see that properly encapsulates the ...
  • 549
0 votes
0 answers
48 views

Vue.js how to access component state/variables but keep encapsulation/hide from template

Vue automatically exposes all methods to the template (using Options API) and all top-level variables (using Composition API). If I need to access logic that is only internal to the script, but wants ...
  • 317
0 votes
1 answer
49 views

How do I, in C#, ensure I never assign to a computed property's backing field directly without encapsulating it in another class?

As properties using custom logic (aka any computed property in c#) need a backing field (source one, two), it can occur that you assign to that backing field directly, changing it's value without ...
  • 445
0 votes
0 answers
21 views

Encapsulated styles in react

I have two react projects (A, B). I have to include the project A inside the project B. A: Made it with CRA, craco and scss modules. B: Made with webpack. The problem is that the styles of the project ...
  • 51
0 votes
2 answers
72 views

Can we say a class with all public member variables and all public methods as encapsulated class?

By definition, encapsulation in Java is a process of wrapping code and data together into a single unit. But if a class has 2 member variables and a method and both the variables and method has public ...
0 votes
2 answers
53 views

What does "Decoupling Implementation Details" mean regarding encapsulation in JavaScript?

I'm reading a blog post regarding encapsulation, after reading it I think I get what encapsulation is and why we need to use it, but at the end, the author talks about what are the advantages of ...
  • 25
0 votes
1 answer
58 views

Get private element outside the class

First of all it's an exercise given to me so i can't change things and have to work with it. I have a 2d vector aka a matrix. My header file looks like this #include <vector> #include <...
0 votes
2 answers
36 views

Private fields accessing inside the class in C# [duplicate]

I have seen a lot of posts suggesting that private fields ought to be accessible from outside via properties (or at least get/set methods). I wonder are there any cases in which we should access our ...
  • 47
3 votes
1 answer
35 views

Why can I assign elements to a list that I doesn't have a setter?

I've been working on a school OOP python project and I stumbled upon this problem: class AList: def __init__(self, l): self.__a_private_attribute = l @property def l(self): ...
0 votes
1 answer
71 views

Is there a way to read the "private Node root" variable from class Pattern?

I want to write a parser for regex, and it would be an easy task, if i could use the tree from Pattern class. I tried to use reflection like this: import org.w3c.dom.Node; import java.lang.reflect....
0 votes
0 answers
42 views

How can I return values where objects are encapsulated in other classes parameters?

public class ONE { private final TWO two; One(final TWO two) { this.two = two; } public TWO two(){ return this.two; } } public class TWO { private final THREE ...
1 vote
0 answers
52 views

Is it possible to encapsulate nested class to not be used outside the outer class?

I converted my JSON to a class in C#, with VisualStudio's "Paste JSON as classes" tool, and this was the result: public class Root { public string orderId { get; set; } public int ...
  • 23
3 votes
1 answer
44 views

How can I access variables in nested structures the same way as unnested variables in Julia?

I am new to Julia, so I am sorry if this is an obvious or easy question. I tried googling it, but could not find a good solution. In Julia, there is no inheritence. My solution to this is to have ...
0 votes
1 answer
64 views

Can an interface be used as a means of encapsulation in terms of clear code? [closed]

I have a Person class. It has private fields name, age and public methods for getting fields (getters). This class also implements interface IGetPublicInfo. public class Person : IGetPublicInfo { ...
-1 votes
1 answer
53 views

Why does inheritance break access rights

I've been experimenting with uniform labyrinth generation, when I've found this problem. So, I've got a labyrinth_builder (lab_bui) class which provides the base for any builder algorithm. class ...
-1 votes
1 answer
44 views

Data privacy and Access Modifiers java

I am currently in the middle of a project, and I have a slight dilemma. Say I have a class called A. In class A, there is a private nested class called B. Then I created another class called C which ...
  • 1
0 votes
1 answer
26 views

How should I use encapsulated variable in outter function in Python without global?

I am trying to write two functions: function_1 is executed inside function_2, however it is using the object var instanced inside function_2. Issue is that var is encapsulated inside function_2 and is ...
  • 15
-2 votes
1 answer
147 views

Object life Time and encapsulation in C++

I have a question about the object life time and assigning values to member variables vs calling member variables via an encapsulation like a getter. In my code I want to update a member variable m_a ...
1 vote
2 answers
100 views

How to achieve encapsulation in C++ project

I'm currently learning about OOP design patterns and I'm working on a project whose main class is roughly organized as follows: class MainClass { public: MainClass(int something, ...
  • 98
0 votes
0 answers
31 views

What does adding 1 underscore before the name of a method (protecting a method) do in Python3? [duplicate]

I know that there isn't truly anything private in Python and protecting a method (adding an underscore at the beginning of its name), however, say I protect my method, does that tell others to not ...
user avatar
0 votes
1 answer
83 views

Creating a copy constructor of a triangle resulting in copy of null error

I'm in an intermediate java class and we have a project that is essentially creating code that checks whether the given values of a triangle creates a proper triangle (positive side lengths and ...
  • 11
2 votes
1 answer
44 views

Are there more implementations of encapsulation?

What I knew about encapsulation in OOP is that it refers to the restriction to the access of some data of an object. I thought that we mainly achieve encapsulation by the usage of access modifiers and ...
  • 33
1 vote
1 answer
33 views

Allow conditional class inheritance

This is a question asked to me in an interview. I have one class say EmployeeClass with two method. EmployeeDetails, SalaryDetails. Now I have two More Class Employee and Hr. My need is when I create ...
  • 11
0 votes
1 answer
36 views

Is encapsulation get achieved when we declare all variables of the instance or class private?

Is encapsulation get achieved when we declare all variables of the instance or class private? What if I have to keep some variables of class public and some private? Is it still an encapsulation? Is ...
0 votes
3 answers
99 views

Confused about encapsulation in Python

I am reading about encapsulation on google from last 3 hours, in some blogs they define encapsulation as the bundling of data members and methods into a single unit, while in some places they confuse ...
-1 votes
1 answer
17 views

Use Encapsulation method how to write the program

Example Program 1 Step 1: Create a class 'Number' Step 2: Declare Non-Static field/variable. Step 3: Create another class ‘EncapTest’. Step 4: Create an object of class Number. Step 5: Print the ...
0 votes
0 answers
19 views

encapsualtion and access specifier [duplicate]

why we provide private for the state's of a class and use getter and setter to access the value rather than using private why aren't we using it as public class user{ private String userName; public ...
-2 votes
1 answer
36 views

setattr() usage in this line

I got this code from somewhere but I don't understand this line of it: Dropdown.bind(on_select = lambda instance, x: setattr(mainbutton, 'text', x)) import kivy from kivy.app import App kivy.require(&...
3 votes
1 answer
207 views

Composite Dto update, best practice

I have a composite Dto, where properties are primitive values or other Dtos (that occurred in many places). Eg : PersonDto Name Surname AddressDto Street Number ContactDto PhoneNo EMail In this ...
  • 1,217
2 votes
2 answers
225 views

Classes and packages encapsulation in an hexagonal architecture

I would like to know if in Java (JDK 17) there is a way to easily handle classes and packages encapsulation in an hexagonal architure. I would like to make unavailable classes present in an adapter to ...
  • 4,030
0 votes
1 answer
62 views

Command design pattern with friend class in C++

The situation: I have to handle and take action on various commands, which can be encapsulated in the command design pattern. So right now I have class Command { virtual void applyCommand(IFoo&...
  • 332
0 votes
1 answer
128 views

How to properly use Dart encapsulation in inheritance

Encapsulation says you should use private attributes, but this brought me an issue. I cannot access super-class attributes in my sub-class. I've readed that if classes are in different files, you ...
0 votes
0 answers
20 views

How do you call a variable from another class's methods in java?

I'm currently trying to print a variable from one class in another class. import java.util.HashMap; import java.util.Random; class Word{ Random r = new Random(); int low = 0; int high = 25; int ...
1 vote
1 answer
161 views

In React, how to encapsulate logic in a Todo component?

In a Todo app made with React, like this one, we have a function toggleTaskCompleted in the App component which gets passed as a prop to each Todo component. This function can then be called in the ...
0 votes
0 answers
44 views

How to encapsulate enterprise level repository for EF Core

After the latest .NET 6 and EF Core release, I want to encapsulate an enterprise level app with generic repository and including asynchronous, perhaps using multiple DbContexts. Which way is better ...
  • 1

1
2 3 4 5
41