Can you give an example of when to use private and when protected?
I'm here until the end can not understand the advantages of using the private access modifier is protected.
After all, one of the principles of SOLID States that in order to change something in the class, no need to change the code - you need to do it through inheritance. Is it possible in this case to use protected?
Can you give an example when to use private functions and have the properties of a class and when to use protected? Can be in any language.
Properties are always private, except may be for example a config array with the default settings, to override the heirs of a property, methods that describe the interface are always public protected if you need to pull them out of the heirs is private methods are helpers that do not belong to the interface and just to do something under the hood
Stacey58 answered on April 19th 20 at 12:33
To change something in the class, no need to change the code - you need to use the pattern decorator.
There is a design principle: "prefer composition over inheritance, and the principle of "design-level abstractions".
Overall, if you can create a class object - it is not necessary to derive better delegate in the new class part of the functionality for that object and add a new functionality, something like that.
Of course, private is needed for cases when anything outside should not leave (except through a getter).
Protected useful if the principle of design-level abstractions when you can figure out the General (and make protected) and separate changing (another principle, by the way).
makenzie answered on April 19th 20 at 12:35
Mommy's defendants, who wrote about the fact that everywhere should be private, clever books I have not read, in addition to dry theory and something, apparently, not developed.
The class almost ALWAYS have to have protected properties. Because they are no different from private, but allow you to use properties in descendant classes, which very often arise. And that would not rewrite the code, it is always better to use protected.
The situation when you really need private-method or property is a very rare phenomenon.