I want to know what is the meaning of protected
in C#, why we use it, and the benefit of the keyword?
For instance
protected int currentColorIndex;
Please elaborate.
I want to know what is the meaning of protected
in C#, why we use it, and the benefit of the keyword?
For instance
protected int currentColorIndex;
Please elaborate.
"A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member."
see
http://msdn.microsoft.com/en-us/library/bcd5672a(VS.71).aspx
It means that the field is only visible to the class itself and inherited classes.
Straight from the MSDN:
The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances.
Using protected means you can have functionality in a class that's available to derived classes, but not to classes that just instantiate the object.
This page compares the different access modifiers and explains what they mean and gives a table of the default modifiers for different objects (enum, class, interface and struct).
Definition provided in another answer. Why is this good? You don't have to duplicate data/code from base class to its derived classes when protected
offers them access to base class implementations, without the unwanted exposure to unrestricted external usage that would be implied by public
.
As others have already pointed out:
The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances.
Here is a small example:
public class A
{
protected string SomeString;
public string SomeOtherString;
}
public class B : A
{
public string Wrapped
{
get { return this.SomeString; }
}
}
...
var a = new A();
var s = a.SomeOtherString; // valid
var s2 = a.SomeString; // Error
var b = new B();
var s3 = b.Wrapped; // valid
Everyone's answer is similar (a definition and/or a excerpt/link to MSDN), so ill attempt to answer your original 3 questions:
The Meaning:
Any field marked with 'protected' means it is only visible to itself and any children (classes that inherit from it). You will notice in the ASP.NET Web Forms code behind model, event handlers (such as Page_Load) are marked 'protected'. This is because the ASPX Markup file actually inherits from the code-behind file (look at the @Page directive to prove this).
Why We Use It:
The common use of the protected accessibility modifier is to give children access to it's parents properties. You might have a base class for which many subclasses derive from. This base class may have a common property. This is a good case for a protected property - to facilitate the re-use and central maintenance of common logic.
The Benefit:
Kind of similar question to "why we use it?" But essentially it gives coarse-grained control over properties. You can't just think of "when you use protected". It's more a case of choosing when to use which accessibility modifier (private, public, internal, protected). So the benefit is really the same benefit of any accessibility modifier - provide a robust and consistent object model, maximising code re-use and minimizing security risks associated with incorrectly exposed code.
Hope that helps.
Think of it like this. A class presents three interfaces:
friend
, of course)!The third interface is the hardest general design challenge in OO: what can reasonably be overridden (virtual methods and properties), and in order to override, what other functionality is needed (plain protected methods and attributes)? Because this is such a challenge, having classes sealed
by default is actually a good idea, counterintuitive as it frequently seems to OO beginners, to whom it seems like an unnecessary handicap.