PROTECTED ACCESS MODIFIER C#

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  .NET 
C#
 509  View(s)
Ratings:
Rate this:
 I want to know what is Protected Access modifier and how to use please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    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();
            }
        }
    }

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!