In OOP, private individuals are private to whom?

In OOP, for example, you need to put something Private. I know that any private member cannot be accessed, but with the same class objects. But why should I do this while I'm the only encoder of my project. The same question applies to Protectedwho!

+5
source share
8 answers

privateand protecteddo not exist, that other coders could not get access to the internal elements of the class, but (also) to prevent the creation of programs without clearly defined interfaces.

If every class in your project can modify every other class, you are not only error prone due to the huge state space, but you are also hindering yourself:

  • Changing the implementation (while maintaining the same interface) of any class.
  • Ever introducing anyone not familiar with all the internal structures of all project classes. If you don’t have perfect memory and you can read every line of code you have ever written, including the future.
  • Unit eruption for unit testing
  • /. , , . . , getter/setter.
+11

:

  • , , .

  • .

, .

+7

, , , , , .

+5

"" . "" , , . , .. , , , , python. , (!) , . , . . . , , .

+5

private, . , , , , . (). Car, , Car, , , , Car Car, , , Car, , . private, , .

, protected ( Java, ) , , , . , , , .

+4

: , , , (private), , , , () , , ().

+3

private protected . . , :

- . , . , . . , . . , . ( )

Note. The answers are good, and this answer is to complete them.

+1
source

If you define a member (variable or method) of a class as private, you cannot use it externally using another class using the point operator. Protected helps protect a member variable or method from inheritance.

0
source

All Articles