25

What are the benefits to defining methods as protected in C#?

like :

protected void KeyDemo_KeyPress( object sender, KeyPressEventArgs e ) 
{
    // some code
}

As compared to something like this:

private void FormName_Click( object sender, EventArgs e )
{
    //some code
}

I've seen such examples in many books and I don't understand why and when do they use private vs protected?

eRaisedToX
  • 3,221
  • 2
  • 22
  • 28
Sherif
  • 1,249
  • 4
  • 15
  • 38
  • but in these example of this book there is no inheritance. – Sherif May 30 '09 at 17:58
  • plz i can't understand we they did so.... – Sherif May 30 '09 at 17:58
  • Can you derive a form from this class? Then you may want to be able to call KeyDemo_KeyPress from that class, which would be impossible if it was private. But since you may not want other classes to be able to access the function, it can't be public. Hence, it is protected. – Hans Van Slooten May 30 '09 at 18:09

6 Answers6

90

Protected methods can be called from derived classes. Private methods can't.

That's the one and only difference between private and protected methods.

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • 3
    This should have been marked as the accepted answer! ;) It's important to realise that 'protected' is an access modifier. Here's a comparison of them all: http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx And for all the modifiers: http://msdn.microsoft.com/en-us/library/6tcf2h8w.aspx – Dave R. Jul 24 '09 at 10:44
8

Often 'protected' is used when you want to have a child class override an otherwise 'private' method.

public class Base {
    public void Api() {
       InternalUtilityMethod();
    }
    protected virtual void InternalUtilityMethod() {
       Console.WriteLine("do Base work");
    }
}

public class Derived : Base {
    protected override void InternalUtilityMethod() {
       Console.WriteLine("do Derived work");
    } 
}

So we have the override behavior we know and love from inheritance, without unnecessarily exposing the InternalUtilityMethod to anyone outside our classes.

var b = new Base();
b.Api();  // returns "do Base work"
var d = new Derived();
d.Api(); // returns "do Derived work"
Bruce
  • 8,202
  • 6
  • 37
  • 49
  • 9
    This is incorrect. protected != virtual. Methods must be *virtual* to override them in a derived class. A virtual method can be protected, but it can also be public or internal. A protected method cannot be overridden unless it is virtual. – Josh May 30 '09 at 18:28
  • 3
    You're right I should have also marked them virtual. With that typo fixed, I do still believe this is a common use case for 'protected'. I'll edit the code above... – Bruce May 30 '09 at 18:54
  • 3
    No, it's a common use case for "virtual". The question is about "protected", not "virtual". – Mark Jul 28 '09 at 18:20
4
  1. Protected methods can be accessed by inheriting classes where as private methods cannot.
  2. Keeping in mind that .aspx and .ascx file inherit from their code behind classes (default.aspx.cs), the protected methods can be accessed from within the .aspx/.ascx

Keep this in mind too: If you have a button and that button's OnClick is set to Button_Click

<asp:Button id="btn" runat="server" OnClick="Button_Click" />

then the Button_Click method needs to have at least protected visibility to be accessible by the button.

You could get around this by added the following to you Page_Load method:

btn.Click += new EventHandler(Button_Click);
Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
2

Some aspects of .NET such as ASP.NET create subclasses of your code-behind class at runtime. So an ASP.NET Page class for example inherits from its codebehind class. By making the method protected, the dynamically generated page class can easily hook up a button click event to a protected method in the base class that handles it.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • This is a reasonable example of a particular case where "protected" is used, but does not address the fact that the example is also an *extremely* narrow scope of what protected is for. – Rex M Jul 07 '09 at 21:53
1

If you have an inherited form (or any class for that matter), you would be able to invoke this function from within the sub-class.

Game_Overture
  • 1,578
  • 3
  • 22
  • 34
0

Protected Methods are just like private methods. They could be accessed only by the members of the class. Only difference is unlike private members, protected members could be accessed by the derived classes as well.

Vinay Chanumolu
  • 111
  • 1
  • 1