Use of Protected Methods in C#

Introduction 
 
I was doing some research on the use of protected methods in the inheritance world and was quite impressed with its actual purpose. Let's explore why the protected methods are useful with an example below.
 
Let's say we have a Base Printer class which has a public instance method Print(). We have two derived classes namely BlackWhitePrinter and ColorPrinter. Based on the users choice I would like to do one of the below:
  1. Printer printerObj = new BlackWhitePrinter();  
  2. printObj.Print();  
  3. or  
  4. Printer printerObj = new ColorPrinter();  
  5. printObj.Print(); 
Assume that we implement a factory pattern in order to get the Printer object based on the user preference and we will call the Print method.
 
Our base Printer class will look like this:
  1. public class Printer  
  2. {  
  3.     private string inputData;  
  4.     private string inkType;  
  5.     public void Print()  
  6.     {  
  7.         GetInputData();  
  8.         inkType = FillCatridge();  
  9.         PrintData();  
  10.     }  
  11.   
  12.     private void GetInputData()  
  13.     {  
  14.         Console.WriteLine("Enter the data to be printed ");  
  15.         inputData = Console.ReadLine();  
  16.     }  
  17.   
  18.     private void PrintData()  
  19.     {  
  20.         Console.WriteLine(inputData + " printed in " + inkType);  
  21.     }  
  22.     protected virtual string FillCatridge()  
  23.     {  
  24.         return "Default";  
  25.     }  
  26. }  
Note that we have followed a template pattern in the Print() method of the base class to specify the order of steps while printing.
  1. Get the input data to be printed from the user
  2. We are making use of the protected FillCatridge() method to inject the behavior of the Printer chosen by the user.
  3. We are actually doing the work of printing
Since we need to override only the behavior of filling the cartridge, our derived classes will now look very neat and simple, similar to below:
  1. public class BlackWhitePrinter : Printer  
  2. {  
  3.     protected override string FillCatridge()  
  4.     {  
  5.         return "Black and White";  
  6.     }  
  7. }  
  8.   
  9. public class ColorPrinter : Printer  
  10. {  
  11.     protected override string FillCatridge()  
  12.     {  
  13.         return "Color";  
  14.     }  
  15. }  
Now, let's check the behaviour of our implementation...
  1. Printer blackWhite= new BlackWhitePrinter();  
  2. Printer color= new ColorPrinter();  
  3. blackWhite.Print();  
  4. color.Print();  
And our output will be:
  1. Enter the data to be printed  
  2. Black white data  
  3. Black white data printed in Black and White  
  4. Enter the data to be printed  
  5. colorful data  
  6. colorful data printed in Color    
Conclusion
  1. We have to choose protected methods when we want to inject one of the behaviors to the base implementation from the derived classes.
  2. We are hiding the FillCatridge method to any instance of the Printer/Its derived classes, at the same time allowing the polymorphism of the FillCatridge method in derived classes. 
  3. This also avoids the repetition of the same flow in the derived classes. In future, if we need to add more functionality, for example, getting the mode of print Portable/Landscape from the user, we can introduce a single method in the base class and include it in the Print() method, by which we are eliminating code repetition in the child classes thus avoiding the testing efforts.
Hope this helps!!