Who do we protect our classes with?

I am currently studying C # and I was wondering what is the point of declaring classes / methods private? Who we hide / restrict access to these classes.

Because if someone edited the source, they could just change the tag from private to public. I am not sure how the user will be able to access these methods and what problems he will lead to.

TL; DR; What are access modifiers.

Thanks!

+6
source share
6 answers

Member visibility, as this function is often called, is not a security function. This is a convenience for the programmer, designed to limit interclass dependencies. When declaring a private person, you do not allow another code to access it directly. This has two advantages:

  • if you find that the member variable is being processed the way you did not expect, the amount of code you should check is significantly less if the variable is private.
  • you can change the internal operation of the class (everything that is declared private) without breaking the interface (everything is declared public)

Member visibility is probably the most important language function in the implementation of encapsulation, which is one of the basic principles of object-oriented programming.

+7
source

This is the basic concept of OO - encapsulation and basically has nothing to do with security. Quote from Wikipedia (emphasis mine):

Hiding the internal objects of an object protects its integrity by preventing users from setting the internal data of the component to an invalid or inconsistent state . The advantage of encapsulation is that it can reduce system complexity and, therefore, increase reliability by allowing the developer to limit the interdependencies between component software.

+1
source

It keeps your code in order. You split your code into an open interface and private internal components.

Thus, you can change your inside without fear of breaking code that depends on your class. You can also safely assume that no other code has changed your internal state while you were looking.

+1
source

Access modifiers are used for encapsulation: they allow you to organize your code in packages and classes and have only the β€œofficial” publication interface visible from the outside, hiding the implementation of the part (which you want to do so that you can change it later without telling anyone) .

This is especially (only?) Important when you release code as a library for other programmers. Even if the code is used only in your own program, it helps to structure larger programs into several packages. - Thilo

0
source

You can write code in two ways: for extension and intended for use as is.

You may or may not have access to the source code.

Access modifiers are most important in code intended for extension, where the client programmer (the programmer who extends and uses your code) should have access only to the API that you expose, mainly to the interface that you offer using these access modifiers,

0
source

Access modifiers are used for encapsulation , so the internal objects of the object / class are hidden from external users or classes. This prevents the user from accidentally or intentionally breaking the object / class by changing its instance variables and / or functions.

Encapsulation is an important part of the Open / closed principle , which refers to a class that is β€œopen to extension, butβ€œ closed to modification ”.” This principle allows classes to be expanded without fear that the class API may change in the future.

0
source

Source: https://habr.com/ru/post/908237/


All Articles