Protected Access modifier C#

Total Post:341

Points:2389
C#  .NET 
C#
 761  View(s)
Ratings:
Rate this:
 I want to know what is Protected Access modifier and how to use please help me.
  1. Post:281

    Points:2003
    Re: Protected Access modifier C#

    The protected access modifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Protected_Modifier
    {
        class ProtectedClass
        {
            // String Variable declared as protected
            protected string ComputerName;
            public void print()
            {
                Console.WriteLine("\nMy name is " + ComputerName);
            }
        }

        class MainClass : ProtectedClass // Inherit access class
        {
            static void Main(string[] args)
            {
                MainClass p = new MainClass();
                Console.Write("Enter your CounputerName:\t");
                p.ComputerName = Console.ReadLine(); // No Error!!
                p.print();
                Console.ReadLine();
            }
        }
    }

     

      Modified On Apr-10-2018 06:09:40 AM

Answer