C
C#3w ago
futurized

trying to learn encapsulation

Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them. Publicly accessible methods are generally provided in the class (so-called getters and setters) to access the values, and other client classes call these methods to retrieve and modify the values within the object.
I'm not really understanding this as why can't an unauthorised party just, for example, write obj1.GetAge()? How would encapsulation prevent access?
23 Replies
Somgör from Human Resources
I think getters and setter functions are just a way to protect a value from being modified in unintended ways If you do GetAge you just get the age there's no way to modify it, therefore it's protected from the outside
futurized
futurized3w ago
I don't really get getters and setters either tbh but the focus rn is encapsulation since I haven't been actually taught getters and setters yet in school
Somgör from Human Resources
Or let's say you have a value that can be changed Inside a setter function you can also take all the necessary steps that need to happen after the variable is set, which a user could just skip
futurized
futurized3w ago
I don't get why you would write { get; set; } instead of just nothing
ACiDCA7
ACiDCA73w ago
to stay in you ageexample.. in the setter you could check that the age is not smaller than 0 or bigger than 100.. and in that case insult the user to proivide a real age ;)
Jimmacle
Jimmacle3w ago
it would prevent access to, for example, change the age to something else the reason auto-properties ( {get;set;} ) are used is because in the event you change the implementation of the get/set in the future it doesn't have to change from a field to a property which is more of a breaking change than if it was already a property
futurized
futurized3w ago
i see but what about when it just says { get; set; } what is the point of that Instead of writing nothing example
class Person
{
int Age { get; set; };
}
class Person
{
int Age { get; set; };
}
Somgör from Human Resources
This is probably your answer
futurized
futurized3w ago
I didn't see that yet
Jimmacle
Jimmacle3w ago
basically it gives you an abstraction point where you can change how the value is actually get or set without changing anything about how the calling code uses it the way fields and properties are accessed in the lower level code is significantly different properties are actually secretly methods (one for get and one for set), just easier to write and use
futurized
futurized3w ago
so a field is like if you write
public int Age;
public int Age;
instead but how is it that much different to the programmer writing the code tryna make changes rather than the behind the scenes looking at it as a method or not
Jimmacle
Jimmacle3w ago
it's not, at a high level a public field is the same as a get;set; autoproperty but it's best practice to not use public fields
futurized
futurized3w ago
what I don't get is if we're expected to use get set why does public exist why does readonly exist
Jimmacle
Jimmacle3w ago
in what situations those are just member access modifiers, any member of a class can be public/private/internal/etc readonly is specific to fields and orthogonal to accessibility
futurized
futurized3w ago
how would it even be a breaking change
Jimmacle
Jimmacle3w ago
it's a binary breaking change because the instructions used to get a field and the instructions used to get a property are different that doesn't matter if you recompile your program that uses the library, but that's not always the case
futurized
futurized3w ago
ik ur tryna explain it to me, I am getting some of it but I'm honestly lost 😭
Jimmacle
Jimmacle3w ago
basically just avoid public fields and you will be better off in the long run, even if you don't fully understand the technical reasons for why it's a good idea
futurized
futurized3w ago
hmmm
Somgör from Human Resources
Now I have a question about that :SCfeet: I assumed get and set functions like GetAge and SetAge are more of a Java thing because Java simply lacks auto properties I've never seen people write actual Getter and Setter functions and instead use for example {get;} on a string to prevent it from being modified Like speaking of C# I've not seen many people write actual functions for it The only times I'd actually do that would be in a ViewModel of something
Jimmacle
Jimmacle3w ago
correct, because properties exist to replace explicit get/set methods
Somgör from Human Resources
:soPortuguese: