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
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
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
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
I don't get why you would write { get; set; } instead of just nothing
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 ;)
This
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
i see
but what about when it just says { get; set; } what is the point of that Instead of writing nothing
example
This is probably your answer
I didn't see that yet
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
so a field is like if you write
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
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
what I don't get is
if we're expected to use get set
why does public exist
why does readonly exist
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
how would it even be a breaking change
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
ik ur tryna explain it to me, I am getting some of it but I'm honestly lost 😭
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
hmmm
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
correct, because properties exist to replace explicit get/set methods
:soPortuguese: