Junior — Middle
Какова роль модификатора доступа protected в объектно-ориентированном программировании?
sobes.tech AI
Answer from AI
The protected access modifier in object-oriented programming restricts access to class members so that they are accessible within the class itself and in all its derived (inherited) classes, but not accessible from other parts of the program.
This is convenient when you need to hide implementation details from external code but still allow subclasses to use or modify these members.
Example in C#:
class BaseClass {
protected int protectedValue = 10;
}
class DerivedClass : BaseClass {
public void ShowValue() {
Console.WriteLine(protectedValue); // Access is allowed
}
}
class OtherClass {
void Test() {
BaseClass b = new BaseClass();
// b.protectedValue - error, access is denied
}
}