Friday, May 31, 2013

Protected Access Modifiers

Members declared with protected access modifiers can be accessed from within the class and from any class which derives from the class which defines the members. These members are not visible to external classes which do not derive from the underlying class.

The following example defines a protected variable
strProtectedName.

    class BaseClass
    {
        private string strPrivateName;
        public string strPublicName;
        protected string strProtectedName;
        internal string strInternalName;
        protected internal string strProtectedInternalName;
    }
    //
    class ChildClass : BaseClass
    {
        public string strChildName;
    }

Notice that the protected variable is not visible to the object of the class, which is instantiated in an external class. 



Protected members are visible in the derived class, in the following example; the protected variable strProtectedName is accessed from a derived class.

    class ChildClass : BaseClass
    {
        public string strChildName;
        //
        void CheckProtectedMember()
        {
            ChildClass objChild = new ChildClass();
            objChild.strProtectedName = "Test";
        }
    }


Search Flipkart Products:
Flipkart.com

No comments: