By 22/07/2015 7 Comments

C# Protected Modifier

  • In C# when a base class inherits into its derived class than all members of a base class is being inherited in its derived class except the members with “private” access that means private members cannot be inherited in derived class.
  • If we want that our base class members remain “private”, cannot be accessed outside the class but still they can be inherited than we mark them with the keyword “protected” modifier Or we declare them as “private” and inherit them through “public” properties.
  • Class members with “protected” access are “private” in its own classes (base and derived) but works as “public” for derived classes/inheritance relationship/hierarchy, they can be inherited, accessible in its derived classes and these are “private” or unavailable outside their hierarchy/inheritance relationship.
  • Remember that “protected” members cannot be accessed directly from outside a class, they can be only accessed through a derived class’s “public” members (properties or methods) indirectly because a “protected” member is only accessible inside a derived class and it is technically a “private” member.


using System;
namespace csharpBasic
    // A base class declaration.
    class MarkSheet
        // Fields are declared with protected modifier.
        protected double AspMarks;
        protected double CSharpMarks;

        // void type parametrized method which sets marks for each subject.
        public void SetSubjectsMarks(double aspMarks, double cSharpMarks)
            this.AspMarks = aspMarks;
            this.CSharpMarks = cSharpMarks;

        // double type methods declaration.
        public virtual double GetObtainMarks()
            return this.AspMarks + this.CSharpMarks;

        public double GetPercentage(double obtainMarks, double totalMarks)
            return (obtainMarks / totalMarks) * 100.0;

    // Derived class declaration which is inherited from its base class.
    class BcsMarkSheet : MarkSheet
        double PhpMarks;

        // Parametrized constructor sets marks for each subject. 
        public BcsMarkSheet(double aspMarks, double cSharpMarks, int phpMarks)
            this.AspMarks = aspMarks;
            this.CSharpMarks = cSharpMarks;
            this.PhpMarks = phpMarks;

        // Double type methods.
        public double GetAspMarks()
            return this.AspMarks;

        public double GetCsharpMarks()
            return this.CSharpMarks;

        public double GetPhpMarks()
            return this.PhpMarks;

        public override double GetObtainMarks()
            return this.AspMarks + this.CSharpMarks + this.PhpMarks;

    class Program
        static void Main(string[] args)
            /* An object creation of a derived BcsMarkSheet 
               through derived class constructor. */
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet(85, 70, 60);

            // Following statements are printing marksheet information.
            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1:");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetAspMarks());

            Console.WriteLine("Subject 2: Csharp");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetCsharpMarks());

            Console.WriteLine("Subject 3: Php");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetPhpMarks());

            Console.WriteLine("Obtain marks: {0}", bcsMarkSheet.GetObtainMarks());
            Console.WriteLine("Percentage: {0}", bcsMarkSheet.GetPercentage(bcsMarkSheet.GetObtainMarks(), 300));
        The Output will be:
        Bcs Mark sheet information:
        Subject 1:
        Marks: 85
        Subject 2: Csharp
        Marks: 70
        Subject 3: Php
        Marks: 60
        Obtain marks: 215
        Percentage: 71.6666666666667

Next Tutorial →

← Previous Tutorial

Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .